The timer
System.Windows.Forms.Timer
should never be used, especially in animation (see also my comments to the Answer by Piccadily). It's better to use
System.Timers.Timer
. However, all timers are problematic: did you think what happens if the code is still busy with timer's event handle when a new event is fired. Resolvable, I understand, still an invitation to instability. Best method is a thread with
System.Threading.Thread.Sleep
calls.
Accuracy is not a big problem as the move of the label can be done not per number of event (not fixed shift), but proportionally to the really elapsed time (measured using
System.DateTime.Now
). UI cannot be controlled from within the non-UI thread. You should use
BeginInvoke
of
Invoke
method of Control or
System.Windows.Threading.Dispatcher
. (Actually, same thing for
System.Timers.Timer
.)
See for more detail: Invocation:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
Useful code and explanation for a thread wrapper:
How to pass ref parameter to the thread[
^].
A thread can be used in three ways: 1) a regular thread created by the constructor of
System.Threading.Thread
(see the reference on thread wrapper above); 2) a thread from a thread pool; 3)
System.ComponentModel.BackgroundWorker
.
—SA