How to catch an asynchronous delegate invocation exception?

G

Guest

How to catch an asynchronous delegate invocation exception?

The code:
---------------------------------------
String message = “my messageâ€;
foreach( MyDelegate handler in m_MyDelegate.GetInvocationList() )
{
try
{
handler.BeginInvoke(message, null, null);
}
catch( System.Net.Sockets.SocketException exp )
{
m_MyDelegate -= handler;
}
}
---------------------------------------

Because I’m calling handler.BeginInvoke(), which is asynchronous invocation,
and not handler(message), which is synchronous invocation, the exception is
not arriving to my code and is not cough in my catch.

Is there a way so I will catch this exception in here?
 
N

Nicholas Paldino [.NET/C# MVP]

Sharon,

If you call EndInvoke, any exception that was thrown during the
execution of the asynchronous call will be rethrown, so you can wrap that.

Hope this helps.
 
R

richlm

Maybe less useful than Nicholas's suggestion in your scenario, but you can
also set up a global exception handler, using e.g.

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(MyAppUnhandledException);

// for windows forms:
Application.ThreadException += new
ThreadExceptionEventHandler(MyAppThreadException);

A more complete description/examples here:
http://www.codeproject.com/dotnet/unhandledexceptions.asp
 
G

Guest

Hi Nicholas,

It may be good for me, but when I’m catching the exception I wish to know
which handler caused this exception and to remove it from the invocation list.

Note: The delegate is use for calling a remote object (in other PC even)
using .NET Remoting.

Can you show me a sample doing that?
 
G

Guest

Hi Richlm,

I think it's much to general for me.
I do want to catch exception separately.
 
N

Nicholas Paldino [.NET/C# MVP]

Sharon,

When the exception is thrown, you should be able to get the StackTrace
from the exception. Once you get that, you can go to the bottom of the
stack (where the exception was thrown), and get the MethodInfo instance from
the GetMethod method on the StackFrame instance. From there, you can use
that to dynamically create a delegate of your type that wraps that method
(assuming you have an object reference, if it is an instance method), and
then remove the delegate.
 
R

Richard Blewett [DevelopMentor]

One thing, the StackTrace on the Exception is a string not an actual StackTrace instance. You'll need to create a new instance of the StackTrace class passing int he exception object.

Also, if you are using AsyncCallback to call EndInvoke you will have the delegate instance you need to remove from the Invocation list as thats what you called EndInvoke on and the AsyncCallback is called once per async delegate invocation.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Sharon,

When the exception is thrown, you should be able to get the StackTrace
from the exception. Once you get that, you can go to the bottom of the
stack (where the exception was thrown), and get the MethodInfo instance from
the GetMethod method on the StackFrame instance. From there, you can use
that to dynamically create a delegate of your type that wraps that method
(assuming you have an object reference, if it is an instance method), and
then remove the delegate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top