Search code examples
c#timerwindows-servicestimelapse

Do timers continue if function didn't finish


I'm currently creating a Windows Service which makes use of a timer interval. This timer repeats every second, but it could take longer than one second to finish Elapsed event of the timer, which is created as following:

Timer timer = new Timer();
timer.Enabled = true;
timer.Interval = 1000; // 1 second
timer.Elapsed += myFunction;

Now the simple question: Does this timer continue even though it didn't finished the Elapsed event yet, or does the timer wait and continue when the Elapsed event finished?


Solution

  • The System.Timers.Timer class has an AutoReset Property.

    If set to false, the timer will only run once allowing you to start it again when you function finishes.

    If you set it to true, the timer will not stop and run your function every second unless an exception pops up.

    By default, this property is set to true