如何使WPF崩溃的应用程序立即关闭(立即是关键字)?

时间:2018-09-28 05:54:27

标签: c# wpf

我有一个WPF应用程序,在发生未处理的异常的情况下,我想

  1. 立即关闭旧应用
  2. 然后重新启动应用程序

第二个目的,I can easily listen to the AppDomain.CurrentDomain.UnhandledException event and then restart the same app using the Process.Start

但是,出于第一个目的,我认为在异常发生之后,应用程序将冻结一段时间,直到最终消失。我尝试通过添加

来加快流程
Application.Current.Shutdown();

但是,程序冻结仍然需要相当长的时间,然后才最终关闭。

为什么会这样?

如何使旧应用程序立即关闭?我的应用程序中未处理的异常已经很糟糕,我不需要垂死的应用程序徘徊足够长的时间并使我感到尴尬。

下面是重现该问题的最小示例代码:

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();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试System.Environment.Exit(1);