The usual answer is using timer, but there using
System.ComponentModel.BackgroundWorker
is much easier and more robust.
By the way, don't use timer control (as a rule of a thumb, never), use
System.Timers.Timer
instead. Using the timer is simple, you won't need any extra instruction, just read its help. You need
Control.Invoke
to change your colors, see below.
Anyway, if you wanted to use timer control, its accuracy would not be better then that of thread.
This will give you idea how to work with
BackgroundWorker
:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[
^], and this — how to stop (cancel) the background work:
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/064fa2b3-8969-4060-970e-79785b854083[
^].
Organize infinite loop in
BackgroundWorker.DoWork
, use
System.Threading.Thread.Sleep(1000)
between changes in color. Do color change via
Control.Invoke
. In the loop, the code should check
BackgroundWorker.CancellationPending
in order to support cancellation mechanism described in the reference above.
That's it.
—SA