While debugging, how can I ensure the output witho

2019-08-08 18:35发布

问题:

In the following console application, if I place a breakpoint on the last line Go(); and execute till breakpoint (in debug mode) by pressing F5, the results are not quite different on each run.
Console can be blank (does not output anything):

or it can output just part of the results on another run:

or, on very rare occasions, the results are "complete" ("actualized"):

If I debug by F10 (Step Over) or F11 (Step into), the results of executing are output into console window immediately.

Why are such differences?
Suppose I am debugging an application using 3d party libraries to source codes of which which I do not have aссess.
Is it possible to ensure the output from them without stepping into their code?

The code of console application:

using System;
using System.Threading;

namespace _5_2
{
  class ThreadNaming
 {
    static void Main()
    {
      Thread.CurrentThread.Name = "main";
      Thread worker = new Thread(Go);
      Thread worker2 = new Thread(Go);
      worker.Name = "1111";
      worker.Start();
      //string just4breakPoint = "aaa";
      worker2.Name = "2222";
      worker2.Start();
      Go();
      Console.ReadLine();
    }

    static void Go()
    {
      Console.WriteLine
        ("Hello from " + Thread.CurrentThread.Name);
    }
  }
}

回答1:

"Why are such differences?" - It's because step debugging introduces synchronisation of threads, and possibly changes the order in which things happen.

The fact you are flagging this as a problem may indicate that you require synchronisation in your code.



回答2:

It became clear to me only after discussion that in order to have complete output up to a breakpoint by F5 it is necessary to synchronize output thereafter by executing step debugging by F10 or F11 once. In most cases, this means inserting dummy line of executable code after the line with breakpoint (before or after the line of interest) in order to introduce the ability to synchronize output by means of dummy "stepping" once.