Answering one of the follow-up questions:
InvokeRequred
can be called on any of the controls involved in the currently running UI. Usually it is called on the control which is used in the invocation following under "if".
Now,
InvokeRequred
is not really needed in most cases. If you call it from non-UI thread is will
always return
true
. This method is needed only in one case: you put it in some method which sometimes is called from UI thread, sometimes in some other thread. People mistakenly attribute too much importance to this function.
I explained the mechanism of invocation in detail in my past solutions.
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