Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke and BeginInvoke

Greetings, I am developing some application in C#. At the moment I'm dealing with threading and I have a question that I have in my mind. What is the difference between Invoke and BeginInvoke? I read some thread and I found some useful information here: here

However what is the difference between Invoke and BeginInvoke in the following code:

private void ProcessRoutine()
{
   for (int nValue = StartFrom; nValue <= EndTo; nValue++)
   {
      this.Invoke(this.MyDelegate, nValue);
      //this.BeginInvoke(this.MyDelegate, nValue);
   }
   MessageBox.Show("Counting complete!");
}
private void MessageHandler(int progress)
{
    lblStatus.Text = lblStatus.Text = "Processing item: " + progress.ToString();
    progressBar1.Value = progress;
}

where MyDelegate is a reference to MessageHandler function.

I noticed that using BeginInvoke lblStatus.Text is not refreshed where using Invoke refreshes the label. Additionally I know that Invoke waits for its execution to complete. The most important case I'm interested in is why there is a difference in refreshing label text in this case.

like image 948
niao Avatar asked Dec 15 '09 19:12

niao


People also ask

What is the difference between Invoke and BeginInvoke?

Invoke: Executes on the UI thread, but calling thread waits for completion before continuing. Control. BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.

What is BeginInvoke?

BeginInvoke(Action) Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on. BeginInvoke(Delegate) Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

What is dispatcher BeginInvoke?

BeginInvoke(DispatcherPriority, Delegate, Object) Executes the specified delegate asynchronously at the specified priority and with the specified argument on the thread the Dispatcher is associated with.

What is dispatcher invoke in C#?

Invoke(Action, DispatcherPriority) Executes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with. Invoke(Action) Executes the specified Action synchronously on the thread the Dispatcher is associated with.


1 Answers

To begin, from your link:

  • Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
  • Control.BeginInvoke: Executes on the asynchronous UI thread, and calling thread doesn't wait for completion.

and from MSDN:

BeginInvoke executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

To sum it up, BeginInvoke is asynchronous. When BeginInvoke is called from the UI thread the request will be executed in parallel with the UI thread. Which means it may not execute until after the currently executing method has returned. So in this case the text box will never appear to update because the for loop will not be interrupted, as the calling thread will not wait for this event to be completed before continuing.

Alternatively, Invoke is synchronous. The text box will be updated because the calling thread will wait for the call to complete before continuing execution.

like image 109
Justin Ethier Avatar answered Sep 20 '22 23:09

Justin Ethier