M Zeinstra
M Zeinstra

Reputation: 1981

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?

Upvotes: 3

Views: 1666

Answers (2)

Irwene
Irwene

Reputation: 3035

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

Upvotes: 5

Alex
Alex

Reputation: 1461

Yes, it does

If the SynchronizingObject property is null, the Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In this situation, the event handler should be reentrant.

Source

Upvotes: 3

Related Questions