简体   繁体   English

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

[英]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 我有一个WPF应用程序,对于未处理的异常,我想

  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 . 第二个目的, 我可以轻松地监听AppDomain.CurrentDomain.UnhandledException事件,然后使用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();
    }
}

Give System.Environment.Exit(1); System.Environment.Exit(1); a try. 尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM