While debugging, how can I ensure the output without stepping into each line of code?

泄露秘密 提交于 2019-12-11 08:56:28

问题


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.



来源:https://stackoverflow.com/questions/15037105/while-debugging-how-can-i-ensure-the-output-without-stepping-into-each-line-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!