Your problem is the cross-thread operation. What happens in the event handler, can happen on other thread then the UI one.
You can use a different timer, for which this is not a problem:
System.Windows.Forms.Timer
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx[
^].
Be warned though: this timer is very bad, it guarantees no reasonable timing precision. For your purpose it could be good enough though, as you hardly require any serious timing precision.
In all other cases (threading and other timers), you should solve this cross-threading problem, which is a pretty simple thing. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.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