1

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();
    }
}
Graviton
  • 81,782
  • 146
  • 424
  • 602

1 Answers1

2

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

Sham
  • 910
  • 8
  • 15
  • Kindly refer https://stackoverflow.com/questions/905544/whats-difference-between-environment-exit-and-application-shutdown . – Sham Sep 28 '18 at 06:06