How many timers is too many?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
Just curious, is it a good coding practice to keep timers in a project to a minimum, or does it not really matter? I've always tried to use as few as possible, but wondering if I really even need to be too concerned with it?
 
For this general question here are some general considerations:

If you're talking about the Forms.Timer consider that each time a timer ticks it takes a slice of the UI thread for processing the event handler. So 'many' such timers ticking frequently can affect the responsiveness of other UI events, it can get to a point where there simply isn't enough time between the intervals to process everything without 'lagging'. The Timers.Timer will have same problem if you synch it to UI thread. Threading.Timer is purely async of UI thread.

If you 'need' many timers on UI thread there is reason to question why. Timers are not used to measure time, only to raise re-occuring events. Perhaps these multiple objects should be measured with Stopwatch instances instead, where you use a single timer to check how each have elapsed.
 
Back
Top