简体   繁体   English

如何在调试C#和vs2012期间停止秒表

[英]how to stop stopwatch during debugging C# and vs2012

So i have some code that depends upon stopwatch for some calculations. 所以我有一些代码依赖stopwatch进行一些计算。

However once stopwatch starts it doesn't stop even during debugging and it is creating problems for me when i try to debug the code. 然而,一旦秒表启动它甚至在调试期间也不会停止,并且当我尝试调试代码时它会给我带来问题。

Is there any way that i can stop stopwatch from running during debugging. 有没有什么办法可以阻止秒表在调试过程中运行。 I am using VS2012. 我正在使用VS2012。

No you can't. 不,你不能。 Because a stopwatch isn't actually running like a thread that you could pause and resume. 因为秒表实际上并不像你可以暂停和恢复的线程那样运行 It's simply taking the time at start and the time at stop and calculating the difference. 它只是花时间在开始时和停止时间并计算差异。

Maybe if you post your code or concept, we could find a way to solve your problem without a stopwatch? 也许如果您发布您的代码或概念,我们可以找到一种方法来解决您的问题,而无需秒表?

It's probably not what you were hoping for, but you can temporarily do this: 这可能不是你所希望的,但你可以暂时这样做:

stopwatch.Stop();
Debugger.Break();
stopwatch.Start();

you could wrap it in #if DEBUG block if you don't want to worry about accidentally leaving it in. 你可以将它包装在#if DEBUG块中,如果你不想担心意外丢弃它。

You could use the following, if you run using the Debug profile in VS2012: 如果在VS2012中使用Debug配置文件运行,则可以使用以下命令:

#if DEBUG
sw.Stop();
#endif

use System.Diagnostics.Debugger.Break(); 使用System.Diagnostics.Debugger.Break(); for this. 为了这。

Debugger.Break: If no debugger is attached, users are asked if they want to attach a debugger. Debugger.Break:如果未附加调试器,则会询问用户是否要附加调试器。 If yes, the debugger is started. 如果是,则启动调试器。 If a debugger is attached, the debugger is signaled with a user breakpoint event, and the debugger suspends execution of the process just as if a debugger breakpoint had been hit. 如果附加了调试器,则使用用户断点事件发出调试器信号,并且调试器暂停执行该进程,就像调试器断点已被命中一样。

Hope Its Helpful. 希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM