Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using Timers have negative effects on applications?

Tags:

vb.net

I am wondering about the Timer component and what, if any, negative effects occur because of its use or multiple instances of its use. In practice, should there be a limit as to how many timers one should use in a project at one time?

like image 569
Jason Bayldon Avatar asked Jan 14 '13 14:01

Jason Bayldon


2 Answers

Well, everything is relative but a System.Windows.Forms.Timer is a pretty expensive object. It works by creating a hidden window, required to make the underlying winapi SetTimer() function work. This window is not shared, every timer object gets its own window. A window is in general one of the more expensive operating system objects.

So a very hard upper limit is that you can never have more than 10,000 enabled timers. Windows refuses to allow an app to create that many windows. You should stay considerably south of that limitation, given that all of the windows of all of the processes that run in one desktop session need to share a common heap. Or in other words, creating a lot of windows but staying below the 10,000 quota can negatively impact other processes, it can make them fail when the heap is exhausted.

I'd say a reasonable upper limit hovers around 100. That's a large number of moving parts to keep track of in general, assuming that all of these timers have different Tick event handlers. If they don't then you should tackle this a different way, you only ever need one Timer to measure an arbitrary number of intervals. Roughly the same way you keep appointments with single watch on your wrist. You do so by storing the due times in a SortedList and start the timer only for the first one that's due. When it ticks, work off the entries in the list that have the expired due time and repeat. When you add or remove a due time, stop the timer and restart it when there's a new first due time.

like image 92
Hans Passant Avatar answered Nov 01 '22 06:11

Hans Passant


I am assuming you mean the winforms timer object So,

From the Docs:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

When you use this timer, use the Tick event to perform a polling operation or to display a splash screen for a specified period of time. Whenever the Enabled property is set to true and the Interval property is greater than zero, the Tick event is raised at intervals based on the Interval property setting.

So reading that line by line if you start to pack your application with timers, you are quickly going to be racing the interval events for UI render time.

For instance: You have a clock application that uses a timer to run the clock. At each 1 second interval you have the application render the hands.

In this application you also let the user define as many 'alarms' as they want. Each one creating a new timer that will trigger at set times. These alarms are also allowed to be cyclical. That is to say you allow the user to set an 'alarm' that goes off every x seconds.

Now suppose the user has a long running task (access DB, network resource, calculate PI to 1500 chars etc) that happens on a cyclical alarm. Now suppose the user has 10 long running tasks that need to happen in order and need to happen at 3 4 and 5 second intervals.

The behavior of these timers would not be adequate for this application because the following would happen:

  • The clock would stop rendering during the execution of the 'alarms'
  • The alarms may run over one another and thus they would queue up but not happen when they were supposed to happen, because the UI thread is processing all messages synchronously.
  • you end up with an unresponsive UI that does not do what you want.

So to answer as best I can your actual question; there does not necessarily need to be a limit to the amount of timers, just the interval between when they will fire in conjunction with the consideration of the time it will take to process your event handler.

If you are using the timers to fire separate processing threads that are going to come back to the UI thread eventually and make changes, then no there does not feasibly need to be a limit until you run into the upper end of the performance of your target machine. That is to say at some point the amount of timers could be so large that you are calling more timer events and clogging the message queue to the point that the form rendering becomes affected.

So in short:

Negative effects:

  • Timers run in the UI thread so they are blocking
  • they can have unexpected behaviors if your interval is shorter than the amount of time it takes to process your event handler.

In practice the only time you should need to limit your usage of timers, like any component that the user does not control, is if they begin to affect the user experience.

I hope that reads a lot less 'ramble-y' than it felt when I was writing it.

like image 31
Pow-Ian Avatar answered Nov 01 '22 07:11

Pow-Ian