Superman
Superman

Reputation: 3784

Getting a .Net application to close on crash

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

Answers (3)

Matt
Matt

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.

Unhandled Exceptions MSDN

Edit: removed bit about Handled flag.. thanks Alex

Upvotes: 4

Alex Aza
Alex Aza

Reputation: 78457

Adding this line will prevent showing 'debug window'.

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

Upvotes: 1

Jeff
Jeff

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

Related Questions