What is "InvokeRequired" about, and what does "BeginInvoke" do?

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

I have the following code:

if (this.InvokeRequired)
{
IAsyncResult result = this.BeginInvoke(new
EventHandler(this.UpdateButtons),
new object[] { this, EventArgs.Empty });

while ((!result.IsCompleted) &&
(result.AsyncWaitHandle.WaitOne(100, false)))
{
//Waitting for the asynch call to complete.
}
this.EndInvoke(result);
}
else
{
UpdateButtons(this, EventArgs.Empty);
}

Could anyone tell me what "InvokeRequired" is about, and what
"BeginInvoke" does?

Thanks!
 
It is used to check whether the current thread is not the UI thread. If it
is not, invoking is required in order to send the command to the UI thread.
Updates to the UI thread (such as a lable or button text) cannot be made from
other threads. It is not required in all cases, so presumably whoever coded
this felt that UpdateButtons needed to be on the UI thread.
 
Thanks for the explanation! It's clear.

The online help is not nearly as clear as yours.
 
Back
Top