It all goes as expected. Let's see. This type of timers invokes you handlers of its event
Elapsed
in a separate thread.
But 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[
^].
By the way, in most cases it's better not to use any timers at all, and, instead, create a separate thread (or leverage one from the thread pool, depending on what you are doing) and call some method in a loop, with some delay. This is much safer and easier to implement. You will understand why if you ever, for example, dealt with the problem when a new timer event is invoked while the handling of the previous timer tick is not yet complete.
—SA