Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing code on application crash

Tags:

c#

crash

I have some clean up code that I would like to execute at the end of execution even if my C# application crashes (delete temp files, etc). Is that possible to do in C#?

Thanks!

like image 327
Warpin Avatar asked Mar 19 '10 22:03

Warpin


4 Answers

It depends on what you mean by "crash".

If you want to deal with any unhandled exceptions that would otherwise bring down an application, you can attach an event handler to the AppDomain.UnhandledException event and then handle any errors in the event handler.

Additionally, in .Net 4.0 there is an AppDomain.FirstChanceException that is called before any catch blocks are executed when an exception occurs.

It is possible however that a real crash can occur (such as an exception being thrown from a finalizer) that causes a fatal application exit that cannot easily be handled, but in most cases the AppDomain.UnhandledException event will probably suffice.

like image 190
adrianbanks Avatar answered Nov 07 '22 22:11

adrianbanks


If your application crashes then it isn't safe to try and execute cleanup code, you don't know what has been corrupted and until your application actually exits, files may be locked, etc.

So I would suggest that you put this cleanup code on application startup. Have your application look for temp files etc on startup before it gets around to creating it's normal temp files, if it finds them, then it can either delete them or attempt to fix/re-use them.

To facilitate this, you can have your application write a log of temp files that it creates and delete that log file when it shuts down successfully. That way when you find the log file on startup, you know that the last run was a crash and that you have to do your cleanup.

Of course, this gets more complicated if you allow several instances of your application to run at once, but the same goes for cleaning things up on crash as well.

like image 45
John Knoeller Avatar answered Nov 08 '22 00:11

John Knoeller


There are several events that you can subscribe to in order to get notification of exceptions.

  • AppDomain.UnhandledException - Triggered by unhandled exceptions in the app domain.
  • Dispatcher.UnhandledException - Triggered by unhandled exceptions on a WPF dispather thread.
  • Application.ThreadException - Triggered by unhandled exceptions on background threads.

Also, as others have mentioned, a finally block around a single entry point will be hit for any exceptions on the main thread.

[I wouldn't recommend trying to recover your application from these handlers though. Particularly in the case of the AppDomain.UnhandledException event, your app is already on the way out, you should just let it shut down. Only use these handlers for doing last minute clean up or logging.]

like image 4
Simon P Stevens Avatar answered Nov 08 '22 00:11

Simon P Stevens


If it is critical that your app cleans up, you can create a monitor app that would be what the user actually launches. It would launch your main app and then monitor its process handle. If your main app crashes, its process handle will signal and your monitor app can clean up and even restart the main app if you wish. I usually combine this with a named mutex that is used to signal back to the monitor app that the main app wishes to shut down, so that it won't restart it.

like image 1
rjones54 Avatar answered Nov 07 '22 23:11

rjones54