有时,在不可重现的情况下,我的WPF应用程序会在没有任何消息的情况下崩溃。应用程序会立即关闭。

哪里是实现全局Try/Catch块的最佳位置。至少我必须实现一个消息框:“抱歉给您带来不便……”


当前回答

如上所述

Application.Current.DispatcherUnhandledException将无法捕获 从另一个线程抛出的异常,然后从主线程抛出。

这实际取决于线程是如何创建的

Application. current . dispatcherunhandledexception没有处理的一种情况是System.Windows.Forms.Timer。可以使用ThreadException来处理这些 如果在主线程以外的其他线程上运行窗体,则需要设置Application。来自每个这样的线程的线程异常

其他回答

如上所述

Application.Current.DispatcherUnhandledException将无法捕获 从另一个线程抛出的异常,然后从主线程抛出。

这实际取决于线程是如何创建的

Application. current . dispatcherunhandledexception没有处理的一种情况是System.Windows.Forms.Timer。可以使用ThreadException来处理这些 如果在主线程以外的其他线程上运行窗体,则需要设置Application。来自每个这样的线程的线程异常

您可以处理AppDomain。UnhandledException事件

编辑:实际上,这个事件可能更合适:应用程序。DispatcherUnhandledException

这里有一个完整的解决方案

它用示例代码解释得很好。但是,要注意它不会关闭应用程序。添加线条 Application.Current.Shutdown (); 优雅地关闭应用程序。

最好的答案可能是https://stackoverflow.com/a/1472562/601990。

下面是一些演示如何使用它的代码:

App.xaml.cs

public sealed partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        // setting up the Dependency Injection container
        var resolver = ResolverFactory.Get();

        // getting the ILogger or ILog interface
        var logger = resolver.Resolve<ILogger>();
        RegisterGlobalExceptionHandling(logger);

        // Bootstrapping Dependency Injection 
        // injects ViewModel into MainWindow.xaml
        // remember to remove the StartupUri attribute in App.xaml
        var mainWindow = resolver.Resolve<Pages.MainWindow>();
        mainWindow.Show();
    }

    private void RegisterGlobalExceptionHandling(ILogger log)
    {
        // this is the line you really want 
        AppDomain.CurrentDomain.UnhandledException += 
            (sender, args) => CurrentDomainOnUnhandledException(args, log);

        // optional: hooking up some more handlers
        // remember that you need to hook up additional handlers when 
        // logging from other dispatchers, shedulers, or applications

        Application.Dispatcher.UnhandledException += 
            (sender, args) => DispatcherOnUnhandledException(args, log);

        Application.Current.DispatcherUnhandledException +=
            (sender, args) => CurrentOnDispatcherUnhandledException(args, log);

        TaskScheduler.UnobservedTaskException += 
            (sender, args) => TaskSchedulerOnUnobservedTaskException(args, log);
    }

    private static void TaskSchedulerOnUnobservedTaskException(UnobservedTaskExceptionEventArgs args, ILogger log)
    {
        log.Error(args.Exception, args.Exception.Message);
        args.SetObserved();
    }

    private static void CurrentOnDispatcherUnhandledException(DispatcherUnhandledExceptionEventArgs args, ILogger log)
    {
        log.Error(args.Exception, args.Exception.Message);
        // args.Handled = true;
    }

    private static void DispatcherOnUnhandledException(DispatcherUnhandledExceptionEventArgs args, ILogger log)
    {
        log.Error(args.Exception, args.Exception.Message);
        // args.Handled = true;
    }

    private static void CurrentDomainOnUnhandledException(UnhandledExceptionEventArgs args, ILogger log)
    {
        var exception = args.ExceptionObject as Exception;
        var terminatingMessage = args.IsTerminating ? " The application is terminating." : string.Empty;
        var exceptionMessage = exception?.Message ?? "An unmanaged exception occured.";
        var message = string.Concat(exceptionMessage, terminatingMessage);
        log.Error(exception, message);
    }
}

Application.Dispatcher.UnhandledException的一个快速代码示例:

public App() {
    this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}

void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
    string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
    MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
    // OR whatever you want like logging etc. MessageBox it's just example
    // for quick debugging etc.
    e.Handled = true;
}

我在App.xaml.cs中添加了这段代码