Coder Perfect

What is the difference between BeginInvoke() and Invoke()? ()


I’m curious as to what the distinction is between BeginInvoke() and Invoke().

Specifically, what each would be utilized for.

EDIT: What’s the difference between establishing a threading object and then doing invoke on it vs. simply calling BeginInvoke() on a delegate? or do they refer to the same thing?

Asked by Nathan W

Solution #1

Are you referring to Delegate? Control or Invoke/BeginInvoke Invoke/BeginInvoke?

Tim’s answer mentions when you might want to use BeginInvoke – although it was mostly geared towards Delegate. BeginInvoke, I suspect.

BeginInvoke is the most common method for Windows Forms apps, in my opinion. You won’t have to worry about deadlock this way, but you should be aware that the UI may not have been refreshed by the time you look at it again! In particular, you shouldn’t modify data which the UI thread might be about to use for display purposes. For example, if you have a Person with FirstName and LastName properties, and you did:

person.FirstName = "Kevin"; // person is a shared reference
person.LastName = "Spacey";
person.FirstName = "Keyser";
person.LastName = "Soze";

The user interface may then display “Keyser Spacey.” (There’s a chance it’ll show “Kevin Soze,” but only because of the memory model’s oddity.)

Control, on the other hand, isn’t necessary unless you’re dealing with something like this. BeginInvoke is less difficult to get correctly, because it prevents your background thread from waiting for no reason. It’s worth noting that the Windows Forms team has assured you that you’ll be able to use Control. BeginInvoke without ever calling EndInvoke in a “fire and forget” fashion. This isn’t true for all async calls: every BeginXXX should have a corresponding EndXXX call, which is usually in the callback.

Answered by Jon Skeet

Solution #2

In response to Jon Skeet’s response, there are occasions when you wish to invoke a delegate and wait for it to complete execution before continuing with the current thread. In those circumstances, the Invoke method is appropriate.

You may not want a thread in a multi-threading program to wait for a delegate to finish execution, especially if the delegate performs I/O. (which could make the delegate and your thread block).

BeginInvoke might be handy in certain situations. You notify the delegate to start by calling it, but your thread is free to perform other things while the delegate is running.

BeginInvoke adds to the complexity of your code, but there are situations when the increased performance is worth it.

Answered by Tim Stewart

Solution #3

The distinction between Control and Command. Control and Invoke() are two functions that can be used together. BeginInvoke() is a method for invoking a program.

A natural conclusion is that a delegate passed to Invoke() can have out-parameters and a return value, whereas a delegate passed to BeginInvoke() can’t (you have to use EndInvoke to retrieve the results).

Answered by Sujit

Solution #4

To demonstrate the impact of their differences, I’ll use a simple, functional example.

new Thread(foo).Start();

private void foo()
        myTextBox.Text = "bing";

BeginInvoke causes the MessageBox to appear at the same time as the text update. When using Invoke, the MessageBox appears after a 3-second nap. As a result, the effect of an asynchronous (BeginInvoke) and synchronous (Invoke) call is demonstrated.

Answered by KMC

Solution #5

Delegate. BeginInvoke() asynchronously queues a delegate’s call and instantly returns control. When working with Delegate. You should use Delegate instead of BeginInvoke(). To receive the results, use EndInvoke() in the callback method.

Delegate. Invoke() calls the delegate synchronously in the same thread.

MSDN Article

Answered by Aaron Palmer

Post is based on