Graviton
Graviton

Reputation: 83254

How to make WPF crashing application shut down immediately ( immediately is the keyword)?

I have a WPF application, which in the case of unhandled exception, I want to

  1. Shut down the old application immediately.
  2. And relaunch the application

For the second purpose, I can easily listen to the AppDomain.CurrentDomain.UnhandledException event and then restart the same app using the Process.Start.

But for the first purpose, I that after the exception occurs, the application will freeze for quite a while before finally going away. I try to speed up the process by putting in the

Application.Current.Shutdown();

And yet, it still takes quite a while for the program to freeze and then only finally shut down.

Why is this the case?

And how I can make the old application shut down immediately? An unhandled exception in my application is already bad enough, I don't need the dying application to linger long enough and to embarrass me.

Here's the minimum sample code that reproduces the problem:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);


        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        DispatcherUnhandledException += CurrentApplicationOnDispatcherUnhandledException;



    }

    private void CurrentApplicationOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {

    }

    private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {

        Application.Current.Shutdown();


    }
}


public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        throw new NotFiniteNumberException();
    }
}

Upvotes: 1

Views: 588

Answers (1)

Sham
Sham

Reputation: 930

Give System.Environment.Exit(1); a try.

Upvotes: 2

Related Questions