So I setup a System.Timers.Timer
and have a handler for the Elapsed
event. It works as expected.
However, if I want to debug the code that is called within the elapsed handler; the timer will continue generating additional events. Thus, I can't single step through the code because timer events stack up on top of each other.
The current workaround is to call Stop
on the timer upon entering the handler and Start
when leaving. However, that is not how the system is supposed to work so that is a temporary fix. I was wondering if there is a way to configure the debugger to halt the timer while debugging.
To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.
Stop Debugging terminates the process you are debugging if the program was launched from Visual Studio.
Pause (suspend or break) execution Set breakpoints in the code that you want to examine and wait until one of them is hit. Break program execution with Ctrl+Alt+Break . The debugger will finish the statement that is executing at the moment you pause, and then stop at the statement that should be executed next.
If you want you can wrap this into a #if DEBUG
directive
or you can use System.Diagnostics.Debugger.IsAttached
.
In your Timer.Elapsed
event handler, maybe you can use some preprocessor directives to include code that stops and starts (or disables and enables) the timer:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
#if DEBUG
(source as Timer).Stop();
// or
(source as Timer).Enabled = false;
#endif
// do your work
#if DEBUG
(source as Timer).Start();
// or
(source as Timer).Enabled = true;
#endif
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With