调试器控制台输出的意外顺序



当我运行代码时,行:

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

在其他输出线之前出现。我怎样才能解决这个问题?代码在下面。

这也是我也表明我的意思的简短视频,但是代码在下面。完整的代码在这里。非常感谢。

    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;
            }
        }
    }

一个输出代码使用Debug.WriteLine(),其他输出代码使用Console.WriteLine()。这是将文本发送到输出控制台的两种不同的方式,该输出控制台并行操作,异步和彼此独立。使用Debug.WriteLine通常比Console.WriteLine快,并且赢得比赛,除非您通过在执行断点之前停止延迟比赛。

Debug.WriteLine的速度更快,因为它直接与调试器进行通信,而Console.WriteLine通过写入调试器必须从中读取的管道来绕行。

想象一下发送信件和发送电子邮件之间的区别。即使发送信件后发送了一段时间,该电子邮件也比信件更早到达。

解决方案:始终仅使用Debug.WriteLine或仅使用Console.WriteLine。不要混合两个。

最新更新