Reputation: 3784
I have a .NET 4 app running on Windows Server 2008 R2 which I use a separate running process to manage its life cycle (i.e., detect turn on / unexpected shutdown / reboot). This works fine for typical conditions. However, when the application throws an exception, Windows brings up the debug window offering to debug the application. I just want the application to crash, so the process runner can detect the crash and manage accordingly.
How do I allow an application to close on an exception?
Upvotes: 3
Views: 1036
Reputation: 339
Add a handler to the Application.ThreadException
and in the handler, log the event, then exit nicely.
Also, add an event handler to AppDomain.CurrentDomain.UnhandledException as well.
Edit: removed bit about Handled flag.. thanks Alex
Upvotes: 4
Reputation: 78457
Adding this line will prevent showing 'debug window'.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
Upvotes: 1
Reputation: 14279
You should add a global exception handler or a try/catch to clean up the resources, log the error and close the app normally.
Upvotes: 0