简体   繁体   中英

how to stop stopwatch during debugging C# and vs2012

So i have some code that depends upon stopwatch for some calculations.

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.

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.

You could use the following, if you run using the Debug profile in VS2012:

#if DEBUG
sw.Stop();
#endif

use System.Diagnostics.Debugger.Break(); for this.

Debugger.Break: If no debugger is attached, users are asked if they want to attach a debugger. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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