1

I have a handler for unhandled exceptions in the App class of my project. It is for the purpose of setting up a Send Feedback button.

App.xaml.cs

private void Application_DispatcherUnhandledException(
    object sender, DispatcherUnhandledExceptionEventArgs e)
{
    try
    {
        // Try to collect feedback with dialog.
        new ApplicationExceptionDialog(e).ShowDialog();
    }
    catch
    {
        MessageBox.Show(
            "A problem has occurred with the program:\r\n\r\n" +
            e.Exception.Message + "\r\n\r\n" +
            "The application will now exit.",
            "Application Error",
            MessageBoxButton.OK,
            MessageBoxImage.Error);
    }
    finally
    {
        Application.Current.Shutdown(1); // ← Is this okay?
    }
}

Question

Other than handling various exceptions, when all else has failed, what is the proper way to exit the application after an unhandled exception?

Edit

This question is specific to WPF applications. I also have unmanaged code in the application to make logins more secure. I haven't had any problems with it, but I need to make sure that if problems arise, the exit process used is the best option.

Tyler Pantuso
  • 835
  • 8
  • 16
  • Possible duplicate: [Terminate application after unhandled exception](http://stackoverflow.com/questions/2266291/terminate-application-after-unhandled-exception) – Rahul Tripathi Feb 18 '16 at 07:42
  • @ RahulTripathi : That question is about message boxes popping up, and the accepted answer uses `Process.Kill()`. Do you agree with that answer? – Tyler Pantuso Feb 18 '16 at 07:50

2 Answers2

3

The proper way to close a WPF app is to use Application.Shutdown. This instructs all message pumps to stop, and allows your clean-up code (such as closing events) to execute. It also leaves all non-background threads running. In your case, this is probably not what you want.

Since you have an unhandled exception, your system is in an unknown state - you can't assume your closing events will run correctly, and that any running threads will gracefully terminate. As such, I'd suggest using Environment.Exit, this will close the process, and all of it's running threads. But it will run your finally blocks and class finalizers, which are typically used to release acquired resources.

If you think that running finalizers in the current state may result in existing data/resource corruption, or poses any other kind of damage to the system, you should use Environment.FailFast instead. This will instruct the OS to simply kill your process immediately, without running any finalizers. You may want to use this over Environment.Exit, depending on the purpose of your application.

Gediminas Masaitis
  • 3,172
  • 14
  • 35
1

To exit a console application try

Environment.Exit(0);

For a windows application try:

Application.Exit(0);
  • I am running a WPF application. I am familiar with the given methods for closing an application when it is running correctly, but not when exceptions remain unhandled. I'll post an edit to the question so there's no more misunderstandings. Thanks Evgenii! – Tyler Pantuso Feb 18 '16 at 07:32