2017-03-07 71 views
-1

的意外的順序當我運行我的代碼,行:調試器控制檯輸出

WriteLine("Saved Files " + saveFiles + "\n"); 

它前面的其它輸出線之前出來。我怎樣才能解決這個問題?代碼如下。

這裏也是一個簡短的video我展示了我的意思,但代碼如下。完整代碼here。非常感謝。

using DemoMemento; 
    using System.Windows; 
    using static System.Diagnostics.Debug; 

    // This Memento patter will create a caretaker that contains the collection 
    // with all the Statements in it. It can add and 
    // retrieve Statements from the collection 

    namespace Memento 
    { 
     /// <summary> 
     /// Interaction logic for MainWindow.xaml 
     /// </summary> 
     public partial class MainWindow : Window 
     { 

      Caretaker caretaker = new Caretaker(); 

      // The originator sets the value for the statement, 
      // creates a new memento with a new statement, and 
      // gets the statement stored in the current memento 

      Originator originator = new Originator(); 

      int saveFiles = 0, currentStatement = -1; 

      // --------------------------------------------- 

      public MainWindow() 
      { 
       InitializeComponent(); 
      } 

      private void btnSave_Click(object sender, RoutedEventArgs e) 
      { 
       // Get text in TextBox 
       string text = theStatement.Text; 

       // Set the value for the current memento 
       originator.set(text); 

       // Add new statement to the collection 
       caretaker.addMemento(originator.storeInMemento()); 

       // saveFiles monitors how many statements are saved 
       // Number of mementos I have 
       saveFiles++; 
       currentStatement++; 

       WriteLine("Saved Files " + saveFiles + "\n");   

       btnUndo.IsEnabled = true; 
      } 

      private void btnUndo_Click(object sender, RoutedEventArgs e) 
      { 
       if (currentStatement >= 1) 
       { 
        currentStatement--; 

        string textBoxString = originator.restoreFromMemento(caretaker.getMemento(currentStatement)); 

        theStatement.Text = textBoxString; 

        btnRedo.IsEnabled = true; 
       } 
       else { 
        btnUndo.IsEnabled = false; 
       } 
      } 

      private void btnRedo_Click(object sender, RoutedEventArgs e) 
      { 
       if ((saveFiles - 1)> currentStatement) 
       { 
        currentStatement++; 

        string textBoxString = originator.restoreFromMemento(caretaker.getMemento(currentStatement)); 

        theStatement.Text = textBoxString; 

        btnUndo.IsEnabled = false; 
       } 
       else 
       { 
        btnRedo.IsEnabled = false; 
       } 

       btnUndo.IsEnabled = true; 
      } 
     } 
    } 
+4

'System.Threading.Thread.Sleep(500);'在UI線程中永遠不會是正確的事情。此外,您需要提出您的問題[最小完整可驗證示例](http://stackoverflow.com/help/mcve),否則您的問題可能會被關閉。應該不需要轉到「完整代碼」鏈接,以便我們能夠幫助解決您的問題,所有內容都應該位於代碼段內,並且應該是重新創建問題的最小可能代碼段。 –

+0

我在帖子中刪除了那條睡眠線,但我估計你的速度不夠快,看不到它。這是一個臨時解決方案。 – myfunnyfella

回答

2

的一個輸出代碼使用Debug.WriteLine(),其他使用Console.WriteLine()。這是將文本發送到並行運行的輸出控制檯的兩種不同方式,它們是異步且彼此獨立的。使用Debug.WriteLine通常會比Console.WriteLine更快並贏得比賽,除非您在執行之前通過在斷點處停止延遲。

Debug.WriteLine更快,因爲它直接與調試器通信,而Console.WriteLine通過寫入調試器必須讀取的管道繞行。

想象一下發送信件和發送電子郵件的區別。即使郵件在寄出後一段時間發送,郵件也比信件早到達。

解決方案:始終只使用Debug.WriteLine或僅使用Console.WriteLine。不要混合兩者。