Re: Delegate.BeginInvoke and EndInvoke.

"Bhatnagar, Amit" <[email protected]> Tue, 15 Jan 2008 11:33:30 -0700
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
Thanks for your response.

Do you know if the AsyncCallback would get called if it times out? If
so, then I could pass the delegate down to the callback and call
EndInvoke.

Every example of timing out a method, using BeginInvoke and EndInvoke,
doesn't make a call to EndInvoke in the timed-out case.

see:
http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html
http://www.eggheadcafe.com/tutorials/aspnet/847c94bf-4b8d-4a66-9ae5-5b61
f049019f/basics-make-any-method-c.aspx


-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Peter Ritchie
Sent: Tuesday, January 15, 2008 11:08 AM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Delegate.BeginInvoke and EndInvoke.

The only EndInvoke that I know of where you're not "required" to call it
is Control.EndInvoke.

See the comments in
http://blogs.msdn.com/cbrumme/archive/2003/05/06/51385.aspx

There are various object that are kept around until EndInvoke is called,
depending on the delegate this may include return value, exception(s),
state, etc.

In your timed-out case, you'll force the thread to block until the
delegate has completed running, which probably defeats the purpose of
timing-out.

You could spawn another thread that waits on the AsyncWaitHandle and
simply calls EndInvoke when it becomes signaled.  That way you free your
current thread to time-out and the EndInvoke is handled in the
background.  It could very well be there's an example of this
somewhere...

On Tue, 15 Jan 2008 10:17:09 -0700, Bhatnagar, Amit
<[email protected]> wrote:

>I have made a service to timeout a method call by launching a delegate 
>asynchronously via BeginInvoke. The documentation states, indirectly, 
>that the call to BeginInvoke should be followed by a call to EndInvoke 
>to prevent leaks. However, I am not sure if the call to EndInvoke is 
>required should the delegate timeout because the call to EndInvoke will

>block the thread and wait for the delegate to complete. So I think what

>I have is OK, but can anyone confirm if the call to EndInvoke is 
>required if it timesout?
>
>See the following code:
>
>---
>IAsyncResult asyncResult = d.BeginInvoke(null, new object());
>
>if (asyncResult.AsyncWaitHandle.WaitOne(7000, true)) {
>   // Not timed out...
>   d.EndInvoke(asyncResult);
>}
>
>else
>{
>   //Timed out ... EndInvoke required here?
>}
>

The information contained in this e-mail message is PRIVATE. It may contain confidential information and may be legally privileged. It is intended for the exclusive use of the addressee(s). If you are not the intended recipient, you are hereby notified that any dissemination, distribution or reproduction of this communication is strictly prohibited. If the intended recipient(s) cannot be reached or if a transmission problem has occurred, please notify the sender immediately by return e-mail and destroy all copies of this message. 
Thank you.