首页 > 解决方案 > C# .net core 3.1 WPF 无异常关闭

问题描述

我的应用程序 WPF .net core 3.1 在 win10 home 中运行了大约两三天,它被关闭并且没有发现任何异常。我在应用程序中放置了异常处理程序

    void App_Startup(object sender, StartupEventArgs e) 
    {
        Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }
    private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        string error = "An unhandled exception just occured: " + e.Exception.Message +
                Environment.NewLine + "WHERE: " + e.Exception.StackTrace;
        string title = "DispatcherUnhandledException";
        Log.Logger.Fatal(e.Exception, "Current_DispatcherUnhandledException");
        MessageBox.Show(error, title, MessageBoxButton.OK, MessageBoxImage.Warning);
        e.Handled = true;
    }
    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var ex = e.ExceptionObject as Exception;
        string error = "An unhandled exception just occured and application will be exit: " + ex.Message +
                Environment.NewLine + "WHERE: " + ex.StackTrace;
        string title = "UnhandledException";
        Log.Logger.Fatal(ex, "CurrentDomain_UnhandledException");
        MessageBox.Show(error, title, MessageBoxButton.OK, MessageBoxImage.Warning);
    }

标签: c#wpfwindows

解决方案


推荐阅读