在调试时,如何确保输出,而不必进入每一行代码? [英] While debugging, how can I ensure the output without stepping into each line of code?
问题描述
在以下控制台应用程序中,如果我在最后一行Go()上放置一个断点;并按F5键执行到断点(在调试模式下),每次运行结果都不会很大。
控制台可以为空(不输出任何内容):
或者可以在另一次运行时输出结果的一部分:
或者在非常罕见的情况下,结果为完成(实现):
如果我通过F10(Step Over)或F11(Step into)进行调试,执行结果将立即输出到控制台窗口中。
为什么会有这样的差异?
假设我正在调试一个使用3d派对库的应用程序来源代码,我没有这样的代码。
有没有进入他们的代码可以确保他们的输出?
控制台应用程序的代码:
using System;
使用System.Threading;
命名空间_5_2
{
类ThreadNaming
{
static void Main()
{
Thread.CurrentThread.Name = 主要;
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);
}
}
}
为什么会有这样的差异? - 这是因为步骤调试引入了线程的同步,并且可能会改变事情发生的顺序。
将此标记为问题的事实可能表明您需要同步在你的代码中。
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);
}
}
}
"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.
这篇关于在调试时,如何确保输出,而不必进入每一行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!