Do you really understand why you want to use
? These methods are implemented in
(and hence in
in particular) and are only useful
when you call them for Control classes from a thread other than your UI thread
. You don't have to implement those methods and you really cannot do that.
Why do you need those methods? You can never call any UI methods or properties form another thread except invocation methods. The methods
eginInvoke allow you to delegate calling of some other UI-related methods to UI class, not really calling them in your non-UI thread. Instead, the delegate instances and data needed to invoke them (such as values of parameters) are put in some queue supported by the UI thread. The UI thread picks up the elements of the queue and performs the actual call in sync with other UI calls. With
, a calling thread is put to wait state until the call is complete by the UI thread while
returns immediately, when a call is not actually performed.
It is not really important on which UI control is used to call an invocation method; it's only important that this control is a part of currently running UI. Now,
is not really needed when you always call some your method from non-UI thread, which is very often the case — you develop some method specially from calling from some non-UI thread. In this case invoke is always required
. You only need
to call before calling some method if your calling method is itself called from different threads: sometimes from your UI thread, sometimes from some other thread. In first case
will always return
, in the second case — always
. If invoke is not required you still can use invocation methods, but direct call will have better performance.
One side note: for WPF UI the same mechanism can be used via the class
with the invocation methods of the same names. This class can be used for
The remaining question is: can this or similar mechanism be used to invoke some method on an arbitrary thread. The answer is: no, unless you create such mechanism. Here is my Tips/Trick article showing how to achieve that: Simple Blocking Queue for Thread Communication and Inter-thread Invocation