Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop timer while debugging

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.

like image 327
Dunk Avatar asked Mar 05 '12 21:03

Dunk


People also ask

How do I stop a debugging session?

To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.

What does stop debugging do?

Stop Debugging terminates the process you are debugging if the program was launched from Visual Studio.

How do I pause debug in 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.


2 Answers

If you want you can wrap this into a #if DEBUG directive or you can use System.Diagnostics.Debugger.IsAttached.

like image 113
Random Dev Avatar answered Oct 04 '22 06:10

Random Dev


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
    }
like image 35
Cᴏʀʏ Avatar answered Oct 04 '22 06:10

Cᴏʀʏ