There are a few things missing from this example. The first is that if
you are calling BeginInvoke, then you should have a corresponding call to
EndInvoke (as per the documentation in regards to this particular
asynchronous pattern).
BeginInvoke will make the posting of the windows message which causes
the call in the UI thread asynchronous. Execution will return immediately
after the call, and you can ^not^ assume that the delegate was called at
that point (unless you interface with the IAsyncResult instance passed back
from BeginInvoke).
Calling the Invoke method circumvents this, as it will wait until the UI
thread processes the delegate, and in this situation, is the better option,
IMO.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> You need to use BeginInvoke to marshal back to the ui thread.
>
[quoted text clipped - 58 lines]
>> Thanks in advance.
>> Anders.
Hmmm, not sure if I agree with this Nicholas.
<inline>
----- Original Message -----
From: "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Friday, February 22, 2008 1:27 PM
Subject: Re: Threading WinForm
> There are a few things missing from this example. The first is that if
> you are calling BeginInvoke, then you should have a corresponding call to
> EndInvoke (as per the documentation in regards to this particular
> asynchronous pattern).
The only case in the API that am aware of where you call BeginXXX and do
*not* have to call EndXXX is in the case of Control.BeginInvoke. This was
confirmed by the winforms team some time ago (although exactly where escapes
me for now)
> BeginInvoke will make the posting of the windows message which causes
> the call in the UI thread asynchronous. Execution will return immediately
> after the call, and you can ^not^ assume that the delegate was called at
> that point (unless you interface with the IAsyncResult instance passed
> back from BeginInvoke).
The question here is why does the back ground thread care if the UI update
has taken place of not at this point. There may be examples where it does
but I can;t think of one off the top of my head.
> Calling the Invoke method circumvents this, as it will wait until the
> UI thread processes the delegate, and in this situation, is the better
> option, IMO.
The only case where Control.Invoke was preferential that I came across was
with the tablet Ink API that meant you could end up with unresolvable
synchronization issues if you didn;t serialize the UI update with background
thread work. In most cases you potentially leave yourself unecessarily open
to deadlocks as you are wait for other threads to do things that you're not
dependent on.
Just my $0,02
Regards
Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2
>> You need to use BeginInvoke to marshal back to the ui thread.
>>
[quoted text clipped - 58 lines]
>>> Thanks in advance.
>>> Anders.
Nicholas Paldino [.NET/C# MVP] - 22 Feb 2008 16:23 GMT
Richard,
See inline:
> Hmmm, not sure if I agree with this Nicholas.
>
[quoted text clipped - 15 lines]
> confirmed by the winforms team some time ago (although exactly where
> escapes me for now)
You are right here. I haven't seen this before, but I'll acknowledge my
error here. This is the thread that you are referring to:
http://discuss.develop.com/archives/wa.exe?A2=ind0311b&L=dotnet-clr&P=9244
>> BeginInvoke will make the posting of the windows message which causes
>> the call in the UI thread asynchronous. Execution will return
[quoted text clipped - 5 lines]
> has taken place of not at this point. There may be examples where it does
> but I can;t think of one off the top of my head.
If the order of the UI updates is important, then you need to use
Invoke. BeginInvoke can not guarantee the order that the messages will get
processed in. If you have two successive calls to BeginInvoke one after
another, it can't be guaranteed that the first call to BeginInvoke will
necessarily process before the second call to BeginInvoke. While it is
^most likely^ that will be the case when processing the successive calls, it
is not guaranteed.
So depending on what your program is doing, and the information it is
displaying (and the order it displays it in), it might or might not be a
good idea.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
>> Calling the Invoke method circumvents this, as it will wait until the
>> UI thread processes the delegate, and in this situation, is the better
[quoted text clipped - 77 lines]
>>>> Thanks in advance.
>>>> Anders.