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

时间:2017-03-07 23:20:49

标签: c# visual-studio debugging

当我运行我的代码时,行:

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

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

这也是我video的一个简短的here我也在说明我的意思,但代码如下。完整代码this sample。非常感谢。

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

1 个答案:

答案 0 :(得分:2)

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

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

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

解决方案:始终只使用Debug.WriteLineConsole.WriteLine。不要混淆两者。