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 4开发者_如何学Go0 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?
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.
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.
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
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
.
精彩评论