tags:

views:

451

answers:

4

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:

  • I want to update all character's health every minute
  • I want to update all character's energy every 2 seconds
  • I want to update all character's specials every 40 seconds

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?

+1  A: 

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.

John Saunders
+2  A: 

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.

Andrew Keith
A: 

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

PRR
A: 

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.

winSharp93