Determine if Timer is running

user1438082 picture user1438082 · Apr 13, 2014 · Viewed 7.6k times · Source

In this Link it shows how to determine if a timer is running. For me it is not working.

I have the timer declared in a static class as shown

public static class ServiceGlobals // Globals
{

    public static System.Timers.Timer _timer = new System.Timers.Timer();
}

}

On Start-up of my service, I set the timer properties

 protected override void OnStart(string[] args)
        {                  

                    ServiceGlobals._timer.AutoReset = false;
                    ServiceGlobals._timer.Interval = (3000);
                    ServiceGlobals._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
                    ServiceGlobals._timer.Enabled = true;
                    ServiceGlobals._timer.Start(); // Start timer                  
        }

then i check if it is running in one of my methods but even when it is running the code is always false

if (ServiceGlobals._timer.Enabled) // Check if the timer is running
{
    // Return error.........

}

Answer

Steve Wellens picture Steve Wellens · Apr 13, 2014

You must have missed this part of the thread you linked:

"If Timer.AutoReset is true, then Enabled will automatically be set to false the first time the timer expires."