How can you complain about not stopping the timer? In your code, I fail to find the call to
Stop
. Use it:
https://msdn.microsoft.com/en-us/library/system.timers.timer.stop%28v=vs.110%29.aspx[
^].
Now, you cannot use the UI directly in the timer event handler, because it is not be guaranteed to be called in the UI thread. 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[
^].
[EDIT]
There is one alternative: using a timer class of very, very poor quality,
System.Windows.Forms.Timer
:
https://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[
^].
Why? Because it can be used without invocation, because the events are all handled in the UI thread. Not only it leads to very bad accuracy, but time accuracy can be just devastating. Forget about anything periodic: not even close. But it can be good in some rare cases (usually, when you want to handle the event only once), when you have no requirements to time accuracy at all. If this is your case, do it.
—SA