I am trying to read value from control in worker thread.
public void worker()
{
while (true)
{
ewh.WaitOne();
int index = -1;
this.Invoke((MethodInvoker)delegate()
{
index = this.comboBoxSource.SelectedIndex;
});
// using index here
As I understand Invoke is launched asynchronously. Am I right that there is no guarantee at the point where I use index
it Invoke
will finish its work? If so, how can I make Invoke as blocking operation?
As I understand Invoke is launched asynchronously.
No, Invoke
is synchronous, in that it will block until the delegate has completed in the UI thread. It's unfortunate that this isn't clearly documented in Control.Invoke(Delegate)
. The other overload (Control.Invoke(Delegate, object[])
states that it's the implementation of ISynchronizeInvoke.Invoke
which is clearer:
Unlike
BeginInvoke
, this method operates synchronously, that is, it waits until the process completes before returning. Exceptions raised during the call are propagated back to the caller.
See more on this question at Stackoverflow