What's the difference between Invoke() and BeginInvoke()?

Do you mean Delegate. Invoke/BeginInvoke or Control. Invoke/BeginInvoke?

Do you mean Delegate. Invoke/BeginInvoke or Control. Invoke/BeginInvoke?Delegate.

Invoke: Executes synchronously, on the same thread. Delegate. BeginInvoke: Executes asynchronously, on a threadpool thread.Control.

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. Tim's answer mentions when you might want to use BeginInvoke - although it was mostly geared towards Delegate.

BeginInvoke, I suspect. For Windows Forms apps, I would suggest that you should usually use BeginInvoke. That way you don't need to worry about deadlock, for example - but you need to understand that the UI may not have been updated by the time you next look at it!

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"; control. BeginInvoke(UpdateName); person. FirstName = "Keyser"; person.

LastName = "Soze"; then the UI may well end up displaying "Keyser Spacey". (There's an outside chance it could display "Kevin Soze" but only through the weirdness of the memory model. ) Unless you have this sort of issue, however, Control.

BeginInvoke is easier to get right, and will avoid your background thread from having to wait for no good reason. Note that the Windows Forms team has guaranteed that you can use Control. BeginInvoke in a "fire and forget" manner - i.e.

Without ever calling EndInvoke. This is not true of async calls in general: normally every BeginXXX should have a corresponding EndXXX call, usually in the callback.

Very good explanation. – chrissie1 Oct 23 '08 at 13:20 3 @Jon Your knowledge in the framework and programming is amazing. – Nathan W Oct 23 '08 at 13:24 11 @Nathan: Nah.

I only really know the "core" stuff like this. Ask me more detailed things about WinForms, ASP. NET, WCF etc and I fall down very quickly.

I'm quite good at sticking to what I know and letting people assume I know more than that though :) – Jon Skeet Oct 23 '08 at 13:56 9 This answer needs a spoiler alert! – IainMH Jan 16 '10 at 15:41 1 Then why would ppl use Invoke over BeingInvoke? Shouldn't there be some advantages over using Invoke.

Both executes processes in the background, just that one is on the same thread, the other on different thread? – yeeen Oct 8 '10 at 9:23.

Building on Jon Skeet's reply, there are times when you want to invoke a delegate and wait for its execution to complete before the current thread continues. In those cases the Invoke call is what you want. In multi-threading applications, you may not want a thread to wait on a delegate to finish execution, especially if that delegate performs I/O (which could make the delegate and your thread block).

In those cases the BeginInvoke would be useful. By calling it, you're telling the delegate to start but then your thread is free to do other things in parallel with the delegate. Using BeginInvoke increases the complexity of your code but there are times when the improved performance is worth the complexity.

I'm confused at Jon's reply: Control. 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 I don't think Control. Invoke/BeginInvoke executes on the UI thread, they should execute on the thread that owns the Control's handle.( which is not always the UI thread ). See MSDN.

Yes, by "on the UI thread" he means on the particular "UI thread" which owns that particular Control's handle. Typically, there is only one UI thread, but it is possible to have multiple UI threads, and in advanced applications there are reasons why you would want them. Technically, any (normal?) thread could start a UI message pump and become a UI thread--and could later shut down the message pump and no longer be a UI thread.

(I assume that's not something to try on a threadpool thread, though. ) – Rob Parker Sep 28 at 18:19.

Delegate.BeginInvoke() asynchronously queues the call of a delegate and returns control immediately. When using Delegate.BeginInvoke(), you should call Delegate.EndInvoke() in the callback method to get the results. Delegate.Invoke() synchronously calls the delegate in the same thread.

MSDN Article.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions