Of course, 100+ timers is way to many to be reasonable. Let's try to reduce the number of timers. Consider just
one. It will be much better: do all the processing for all the controls in the handler or callback of this timer (there are three type of timer in .NET, the one who will find them all is a cool guy :-)). Depending on what you want to do with all the controls, you can supply an array of all controls, or a class instance with several arrays of controls, one property per control type. I don't want to go into further detail in this solution, as the
number of timers can be further reduced.
Here is the best solution: use
0 (zero) timers! You don't need any timers for such thing, and using them is not easy and error-prone. Instead, use a separate thread. In the thread, you need to sleep for some period of time (
System.Threading.Thread.Sleep
), which uses zero CPU time until a thread is waken up by an expiration of time or
Abort
. When your main form is about to close, just abort this thread using
System.Threading.Thread.Abort
. Don't try to pause/resume thread if you want it. Instead, use the class
System.Threading.ManualResetEvent
to throttle the thread.
See:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[
^].
Now, how this thread can work with UI?
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
—SA