How to handle exception in multicast delegate in C#?
Asked Answered
T

3

5

I've been given some code that I am calling through multicast delegate.

I would like to know how I can catch up and manage any exception raised there and that is not managed for the moment. I cannot modify the code given.

I've been looking around and found about the need to call GetInvocationList() but not really sure if this is helpful.

Tetra answered 21/4, 2011 at 6:59 Comment(2)
Can you post the code you've tried?Simeon
possible duplicate of MulticastDelegate and Exception handling: is it possible to wrap it all generically?Archilochus
K
5

Consider the code using GetInvocationList:

foreach (var handler in theEvent.GetInvocationList().Cast<TheEventHandler>()) {
   // handler is then of the TheEventHandler type
   try {
      handler(sender, ...);
   } catch (Exception ex) {
      // uck
   }
}   

This my old approach, the newer approach I prefer is above because it makes invocation a snap, including the use of out/ref parameters (if desired).

foreach (var singleDelegate in theEvent.GetInvocationList()) {
   try {
      singleDelgate.DynamicInvoke(new object[] { sender, eventArg });
   } catch (Exception ex) {
      // uck
   }
}

which individually calls each delegate that would have been invoked with

 theEvent.Invoke(sender, eventArg)

Happy coding.


Remember to do the standard null-guard copy'n'check (and perhaps lock) when dealing with events.

Keelung answered 21/4, 2011 at 7:9 Comment(2)
Be very careful when using this method: performance wise it's extremely costly. For 10 000 event handlers on one event, this method takes 20ms while the regular method takes 1ms. For 10 000 000 event handlers, it takes 1900ms against 6ms with the old method. You need to take into consideration this and decide if the performance cost is worth it. I decided it was not in my caseKhotan
This doesn't work properly in certain situations when the invoked delegates are modifying the event (adding/removing delegates)! Haven't found a work-around myself yet, but the MulticastDelegate itself is implemented as a linked list which makes it more resistant against dynamic modifications than a flat array.Naze
A
3

You can loop through all the delegates registed in the multicast list and call each of them in turn while wrapping each call in a try - catch block.

Otherwise the invocations of the subsequent delegates in the multicast after the delegate with the exception will be aborted.

Adaiha answered 21/4, 2011 at 7:4 Comment(2)
Thank you Jaapjan. So do you mean that you are doing the same as this example: #4160531Tetra
That is the gist, yes. There are various examples spread around both this topic (now) and the one you refer to.Adaiha
D
0

The upvoted answer is for events, for delegates specifically try this extension method:

    public static class DelegateExtensions
{
    public static void SafeInvoke(this Delegate del,params object[] args)
    {
        foreach (var handler in del.GetInvocationList())
        {
            try
            {
                    handler.Method.Invoke(handler.Target, args);
            }
            catch (Exception ex)
            {
                // ignored
            }
        }
    }
}
Diphyodont answered 17/2, 2016 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.