473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to catch an asynchronous delegate invocation exception?

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?

--
Thanks
Sharon G.
Nov 16 '05 #1
6 15952
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sharon" <Sh****@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
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?

--
Thanks
Sharon G.

Nov 16 '05 #2
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(MyAppUnhandledExcep tion);

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

A more complete description/examples here:
http://www.codeproject.com/dotnet/un...exceptions.asp
Nov 16 '05 #3
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?

------
Thanks
Sharon
Nov 16 '05 #4
Hi Richlm,

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

-----
Thanks
Sharon
Nov 16 '05 #5
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sharon" <Sh****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
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?

------
Thanks
Sharon

Nov 16 '05 #6
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jon Davis | last post by:
If two delegates are created that point to the exact same method, and an event is assigned both delegates, ... myObj.MyEvent += new EventHandler(MyHandler); myObj.MyEvent += new...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
13
by: ALI-R | last post by:
Hi All, When we say events are processed asynchronously in for instance Sharepoint ,what dose it mean? Thanks for your help. Ali
5
by: Steve Murphy | last post by:
I have a couple of standard Try/Catch blocks that I use repeatedly. Is there anyway to wrap these blocks into a method call? Or does C# have anything like a C++ macro? Thanks, Steve Murphy
2
by: Ed A | last post by:
Hi all: My class has one event that gets fired on a timer, typically only one function consumes it on the receiving end. // Source Class public delegate void DataHandler(ArrayList aList);...
3
by: Oscar Thornell | last post by:
Hi, I am thinking about doing all my logging asynchronously using a delegate. The main resaon for this would be performance and responsiveness of the application (an ASP.NET app). //Exampel...
1
by: dba123 | last post by:
I need to perform Asynchronous Inserts using DAAB. So far I have a method which does an insert but how can I do this Asyncronously so that it does not affect the load on our public production...
4
by: 6954 | last post by:
Hi! i need to implement some asynchronous call to my com+ component, but i need it to return some values (e.g. results of sql select statement). obviously queued components and MSMQ are out of...
1
by: Morgan Cheng | last post by:
Asynchronous method model generally has a callback delegate as argument. The delegate will be called when some WaitHandle is set or time out. And, one more argument is used as state object to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.