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!
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!
There's the finally clause of a try block. But there's an interesting discussion over at Eric Lippert's blog about whether or not you want to make the effort, as your application might very well be in a irrecoverable state. http://blogs.msdn.com/ericlippert/archive/2010/02/25/careful-with-that-axe-part-two-what-about-exceptions.aspx
It depends on what you mean by "crash." If you mean an unhandled exception you can create a handler for Application.ThreadException. If the crash is caused outside of managed code, there's nothing you can do.
That's what try {} finally {}
is for. Wrap all your code in your main
method in it, that should do it.
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.
There are several events that you can subscribe to in order to get notification of exceptions.
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.]
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.
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.