What is the working procedure of ISynchronizeInvoke
?
How to work with it in C#?
This basically describes a way to push work between threads; to push an item of work onto the other thread, use either Invoke
(synchronous) or BeginInvoke
(asynchronous - ideally calling EndInvoke
later). Likewise, InvokeRequired
is used to ask "do I need to do this? or can I execute the work myself?".
The most common use of this interface is in windows-forms, where it is part of how to push work onto the UI thread; you can of course use Control.Invoke
/ Control.BeginInvoke
equally, but forms controls implement this interface to allow abstraction - so downstream code doesn't need to tie itself to windows forms. In the case of forms, InvokeRequired
means "am I the UI thread?".
In reality, I'm not sure it is that common to use it directly. It is more common to handle events on the UI, and have the UI handle thread-switching using the most appropriate local mechanism.
Typical usage:
obj.Invoke((MethodInvoker) SomeMethod);
which executes (via a delegate) SomeMethod
on the thread managed by obj
(which implements the interface).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With