TheAJ
TheAJ

Reputation: 10875

.NET - System.Timers.Timer question

I want to ask if a System.Timers.Timer instance runs on a single thread, a thread pool, or something else?

If it does run on a single thread, what do you think would be best for this example:

Should I run them each on a separate thread, run them on a separate event, or run all of those in a single thread having to check the time differences?

Upvotes: 2

Views: 1093

Answers (4)

Andrew Keith
Andrew Keith

Reputation: 7563

I would run all the actions from a single timing thread and compute the time differences. This way I would be able to run as many actions as I would like without instantiating more timers.

It's also easier to synchronize actions which occur at the same time interval.

Upvotes: 2

Matthias
Matthias

Reputation: 12259

Should I run them each on a separate thread, run them on a separate event, or run all of those in a single thread having to check the time differences.

In my opinion you should use three timers. Three is a number which should not affect the performance in a negative way.

A big disadvantage when calculating time differences are time-changes (daylight saving times, synchronisation with the Internet). You would have to use Environment.TickCount.

Upvotes: 0

PRR
PRR

Reputation: 1178

System.Timers.Timer elapsed event uses Threadpool. So multiple threads can be invoked if the interval is less( and elapsed event> interval takes long time to complete). Threadpool runs in background and UI updates cannot be done outside the UI thread in windows application. You can however use Control.Invoke Control.BeginInvoke

Link

Timers

difference between invoke and begininvoke

Upvotes: 0

John Saunders
John Saunders

Reputation: 161773

From http://msdn.microsoft.com/en-us/library/system.timers.aspx:

The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.

Upvotes: 1

Related Questions