Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use ISynchronizeInvoke interface?

Tags:

c#

What is the working procedure of ISynchronizeInvoke?

How to work with it in C#?

like image 417
Jaswant Agarwal Avatar asked Sep 01 '09 05:09

Jaswant Agarwal


1 Answers

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).

like image 175
Marc Gravell Avatar answered Sep 21 '22 00:09

Marc Gravell