473,804 Members | 3,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ge tInvocationList () )
{
try
{
handler.BeginIn voke(message, null, null);
}
catch( System.Net.Sock ets.SocketExcep tion exp )
{
m_MyDelegate -= handler;
}
}
---------------------------------------

Because I’m calling handler.BeginIn voke(), 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 15976
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.co m

"Sharon" <Sh****@discuss ions.microsoft. com> wrote in message
news:F4******** *************** ***********@mic rosoft.com...
How to catch an asynchronous delegate invocation exception?

The code:
---------------------------------------
String message = "my message";
foreach( MyDelegate handler in m_MyDelegate.Ge tInvocationList () )
{
try
{
handler.BeginIn voke(message, null, null);
}
catch( System.Net.Sock ets.SocketExcep tion exp )
{
m_MyDelegate -= handler;
}
}
---------------------------------------

Because I'm calling handler.BeginIn voke(), 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.Curre ntDomain.Unhand ledException += new
UnhandledExcept ionEventHandler (MyAppUnhandled Exception);

// for windows forms:
Application.Thr eadException += new
ThreadException EventHandler(My AppThreadExcept ion);

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.co m

"Sharon" <Sh****@discuss ions.microsoft. com> wrote in message
news:F3******** *************** ***********@mic rosoft.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.co m

Nov 16 '05 #7

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

Similar topics

4
2525
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 EventHandler(MyHandler); .... does it recognize the duplicate method and ignore the second one, or does it execute the same method twice when raised? Jon
3
2328
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. Here's what I'm trying to do... I am iterating through the invocation list invoking each delegate manually. I want to eventually try to invoke a delegate to an object that has been destroyed. I'm essentially looking to get the exception...
13
4400
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
2409
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
14975
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); public event DataHandler DataEvent; ...
3
4510
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 delegate void LogDelegate(string message); LogDelegate logger = new LogDelegate(Log.Debug); logger.DynamicInvoke("Some message..");
1
2880
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 website? This question is wide open but make sure you give me some ideas in context with DAAB syntax. Some thoughts are threading, ATLAS, etc. but I have no clue how to even approach an Asynchronous Insert or any techniques at this point. Also, I...
4
2212
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 the question... anyone has any ideas how to implement it? or just a guideline maybe? thank you
1
1950
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 transfer any customized data into callback delegate. I believe that is un- necessary. For example, public static RegisteredWaitHandle RegisterWaitForSingleObject ( WaitHandle waitObject, WaitOrTimerCallback callBack,
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.