Re: exception handling with events

From: DanGo (dgolick_at_gmail.com)
Date: 12/22/04


Date: 22 Dec 2004 13:14:31 -0800

You must call EndInvoke to avoid memory leaks.
The good news is that any exception that your DoAction threw will be
thrown when you call EndInvoke (that's so cool!)

So when shoud you call EndInvoke?

The second to last argument to BeginInvoke is an AsyncCallback that
will be called when your function has completed. You can use this to
call EndInvoke.

Of course you need the delegate to fire EndInvoke. The delegate is
hidden inside the argument passed to your AsyncCallback method.
IAsyncResult does not contain the delegate but it can be safely cast to
AsyncResult (in the System.Runtime.Remoting.Messaging namespace) this
contains the delegate which
you can then use to call EndInvoke.

You should call EndInvoke in a try/catch block to catch any exceptions
your asynchronous method threw.

Your AsyncCallback executes on the thread pool thread so don't directly
access UI from the callback (use control.BeginInvoke).

Instead of :
server.DoAction.BeginInvoke (param1, param2, null, null);

use :
Server.DoAction.BeginInvome(param1, param2, new
AsyncCallback(ActionCompleted), null);

void ActionCompleted(IAsyncResult iar)
{
System.Runtime.Remoting.Messaging.AsyncResult ar = iar as
System.Runtime.Remoting.Messaging.AsyncResult;
if (iar != null)
{
EventHandler eh = ar.AsyncDelegate as EventHandler;

if (eh != null)
{
try
{
eh.EndInvoke();
}
catch(Exception ex)
{
// the exception thrown by Server.DoAction
// process the exception
// If you want to display it on the ui make sure to use
Control.BeginInvoke since this routine is executing on the thread pool.
}
         }
    }
}



Relevant Pages

  • Re: Garbage collection and async operations
    ... routine or its delegate goes out of scope. ... mechanism is independent of whether or not I call EndInvoke. ... Delegate (which presumably holds the return value from the invoked ... and the Exception object thrown by the invoked routine). ...
    (microsoft.public.dotnet.framework)
  • Re: Keine Ausnahme bei BeginInvoke ohne EndInvoke
    ... public delegate void Foo; ... die in Bar() ausgelöst wird? ... Der Aufruf von asyncronen Delegates cached die Exception, ... Dazu brauchst Du eben das EndInvoke. ...
    (microsoft.public.de.german.entwickler.dotnet.framework)
  • Re: Will Async Callback hander code ever be called if server offli
    ... The docs are correct -- the call to EndInvoke within your callback handler ... will raise an exception if the server is offline. ... When you call EndInvoke, the originally caught exception is rethrown so you ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Keine Ausnahme bei BeginInvoke ohne EndInvoke
    ... EndInvoke aufgerufen werden _muss_ (soweit ich mich an die Doku erinnere, ist das auch für die Resourcenfreigabe wichtig!) und im Fall einer Exception, wird sie bei EndInvoke ausgelöst. ... Wie sonst möchtest du die Exception bekommen? ...
    (microsoft.public.de.german.entwickler.dotnet.framework)
  • Re: BeginInvoke without EndInvoke allowed?
    ... harm in not calling Control.EndInvoke, I would say that absent some clear, explicit statement in the MSDN documentation pages that says that you don't need to call EndInvoke, that pairing the two calls may well be important, and very much _should_ be assumed to be so, ... And with our knowledge of MSDN, even present some clear, explicit statement might not be enough either. ... EndInvoke would impose an unnecessary delay on the background thread where I call BeginInvoke. ... If you call Delegate.BeginInvoke() on the delegate instance itself, ...
    (microsoft.public.dotnet.framework)