2

In my application, I am using the free NBug crash reporting library. In order to integrate it into your project you need to add these two lines in your Program.cs:

AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
Application.ThreadException += NBug.Handler.ThreadException;

However, I need to run a block of code in cases where my program crashes. This code needs to clean up some entries in a mysql database. I thought of simply adding extra event handles after the NBug ones:

AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
Application.ThreadException += NBug.Handler.ThreadException;
Application.ThreadException += new ThreadExceptionEventHandler(mysqlcleanup);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(mysqlcleanup1);

However in this case the program only performs the mysql cleanup events, and ignores the NBug ones. Is it even possible to have multiple exception handlers? I thought of editing the NBug source to run the mysql commands but that would mean attaching the mysql library to that project which is less than ideal.

Any ideas are very welcome.

nerdalert
  • 441
  • 1
  • 5
  • 22
  • 1
    Seems like there must be more to the story. Multiple events, such as you have shown, is fine. – DonBoitnott Sep 25 '15 at 12:29
  • Well what I've come to realize is that it will only perform the last two lines. So if I have the two NBug handlers last, those will fire. If I have the mysql ones last, those will fire. – nerdalert Sep 25 '15 at 12:35

1 Answers1

2

You can try to hook the AppDomain exceptions only to your event handlers. Create then 2 of your own exception events, same signature as the Appdomain's. Hook the NBug to your own events. In your event handlers, do your mysql cleanup and then raise your your own events which should notify NBug. Don't know if this workaround will work...

I will just illustrate one event:

create the following event:

public event UnhandledExceptionEventHandler OnUnhandledException;

Remove the NBug assignments from the Appdomain exceptions, keeping only your own assigned. There you assign the NBug method to your new event, something like this:

OnUnhandledException += Nbug..... 

like you had it assigned to the Appdomains event. Then in your method where you cleanup mysql you implement:

OnUnhandledException(sender, unhandledExceptionEventArgs);

Hope this helps. Note that it is good practise to check first if your event is assigned or not before calling it:

if (OnUnhandledException != null)
OnUnhandledException(sender, unhandledExceptionEventArgs);

but in your case you know it is assigned to NBug.

  • I understand your logic, but I'm struggling to put it in code. I tried moving the NBug handlers into the mysqlcleanup methods but that doesn't work. As you can tell I'm relatively new to C# – nerdalert Sep 25 '15 at 13:07
  • Bear with me, need to type all on mobile phone. Will update answer. –  Sep 25 '15 at 13:29
  • Thanks for the update, now try bear with me haha. I get this error when adding "OnUnhandledException += NBug.Handler.UnhandledException;" Cannot implicitly convert type 'System.UnhandledExceptionEventHandler' to 'System.Action – nerdalert Sep 26 '15 at 07:45
  • Cant see entire response on mobile, compile time error? Ensure the method signatures are the same, will see on monday if you came right. –  Sep 26 '15 at 08:46
  • Yes compile time error. I followed your instruction as best I could, though I may have made an error. – nerdalert Sep 26 '15 at 09:41
  • I mentioned it in my comment above. "Cannot implicitly convert type 'System.UnhandledExceptionEventHandler' to 'System.Action". I'm getting it at line: "OnUnhandledException += NBug.Handler.UnhandledException;" – nerdalert Sep 28 '15 at 06:28
  • Change your own event as follow: public event UnhandledExceptionEventHandler OnUnhandledException; (will update the answer) –  Sep 28 '15 at 07:10
  • 1
    Thanks it works now! However it must be defined as public static event, otherwise there are exceptions. – nerdalert Sep 28 '15 at 09:53