代码之家  ›  专栏  ›  技术社区  ›  Graviton

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

  •  0
  • Graviton  · 技术社区  · 7 年前

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

    1. 关闭旧应用程序 立即 .

    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 回复  |  直到 7 年前
        1
  •  1
  •   Sham    7 年前

    给予 System.Environment.Exit(1);