How to stop the timer during debugging

So, I am setting up System.Timers.Timer and have a handler for the Elapsed event. It works as expected.

However, if I want to debug code that is called in a handler with an elapsed time; the timer will continue to generate additional events. Thus, I cannot take one step through the code, because the timer events are stacked on top of each other.

The current workaround is to call Stop on the timer when entering the handler and Start when exiting. However, this does not mean that the system should work in such a way that it is a temporary solution. I was wondering if there is a way to configure the debugger to stop the timer during debugging.

+10
source share
2 answers

If you want, you can include this in the #if DEBUG directive or you can use System.Diagnostic.Debugger.IsAttached .

+12
source

In your Timer.Elapsed event Timer.Elapsed , perhaps you can use some preprocessor directives to enable code that stops and starts (or disables and enables) a 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 } 
+5
source

All Articles