【问题标题】:How to make WPF crashing application shut down immediately ( immediately is the keyword)?如何使 WPF 崩溃的应用程序立即关闭(立即是关键字)?
【发布时间】:2018-09-28 05:54:27
【问题描述】:

我有一个 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();
    }
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    试试System.Environment.Exit(1);

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-07
    • 2019-01-27
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多