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

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!
 
F

Family Tree Mike

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

Curious

Thanks for the explanation! It's clear.

The online help is not nearly as clear as yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top