37

在阅读了一些关于 AppDomain.UnhandledException 和 Application.DispatcherUnhandledException 之间区别的优秀帖子之后,看来我应该同时处理这两个问题。这是因为用户更有可能从主 UI 线程(即 Application.DispatcherUnhandledException)引发的异常中恢复。正确的?

另外,我是否也应该让用户有机会为两者继续程序,或者只是 Application.DispatcherUnhandledException?

下面的示例代码同时处理 AppDomain.UnhandledException 和 Application.DispatcherUnhandledException,并且都为用户提供了在出现异常时尝试继续的选项。

[谢谢,下面的一些代码是从其他答案中提取的]

应用程序.xaml

<Application x:Class="MyProgram.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="App_StartupUriEventHandler"
         Exit="App_ExitEventHandler"
         DispatcherUnhandledException="AppUI_DispatcherUnhandledException">
    <Application.Resources>
    </Application.Resources>
</Application>

App.xaml.cs [已编辑]

/// <summary>
/// Add dispatcher for Appdomain.UnhandledException
/// </summary>
public App()
    : base()
{
    this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}

/// <summary>
/// Catch unhandled exceptions thrown on the main UI thread and allow 
/// option for user to continue program. 
/// The OnDispatcherUnhandledException method below for AppDomain.UnhandledException will handle all other exceptions thrown by any thread.
/// </summary>
void AppUI_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    if (e.Exception == null)
    {
        Application.Current.Shutdown();
        return;
    }
    string errorMessage = string.Format("An application error occurred. If this error occurs again there seems to be a serious bug in the application, and you better close it.\n\nError:{0}\n\nDo you want to continue?\n(if you click Yes you will continue with your work, if you click No the application will close)", e.Exception.Message);
    //insert code to log exception here
    if (MessageBox.Show(errorMessage, "Application User Interface Error", MessageBoxButton.YesNoCancel, MessageBoxImage.Error) == MessageBoxResult.No)
    {
        if (MessageBox.Show("WARNING: The application will close. Any changes will not be saved!\nDo you really want to close it?", "Close the application!", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
        {
            Application.Current.Shutdown();
        }
    }
    e.Handled = true;
}

/// <summary>
/// Catch unhandled exceptions not thrown by the main UI thread.
/// The above AppUI_DispatcherUnhandledException method for DispatcherUnhandledException will only handle exceptions thrown by the main UI thread. 
/// Unhandled exceptions caught by this method typically terminate the runtime.
/// </summary>
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    string errorMessage = string.Format("An application error occurred. If this error occurs again there seems to be a serious bug in the application, and you better close it.\n\nError:{0}\n\nDo you want to continue?\n(if you click Yes you will continue with your work, if you click No the application will close)", e.Exception.Message);
    //insert code to log exception here
    if (MessageBox.Show(errorMessage, "Application UnhandledException Error", MessageBoxButton.YesNoCancel, MessageBoxImage.Error) == MessageBoxResult.No)
    {
        if (MessageBox.Show("WARNING: The application will close. Any changes will not be saved!\nDo you really want to close it?", "Close the application!", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
        {
            Application.Current.Shutdown();
        }
    }
    e.Handled = true;
}
4

2 回答 2

41
  • AppDomain.CurrentDomain.UnhandledException理论上可以捕获 appdomain 的所有线程上的所有异常。不过,我发现这非常不可靠。
  • Application.Current.DispatcherUnhandledException捕获 UI 线程上的所有异常。这似乎工作可靠,并将替换AppDomain.CurrentDomain.UnhandledExceptionUI 线程上的处理程序(优先)。用于e.Handled = true保持应用程序运行。

  • 为了在其他线程上捕获异常(在最好的情况下,它们在自己的线程上处理),我发现 System.Threading.Tasks.Task(仅 .NET 4.0 及更高版本)维护成本低。使用 方法处理任务中的异常.ContinueWith(...,TaskContinuationOptions.OnlyOnFaulted)有关详细信息,请参阅我的答案。

于 2012-10-22T14:37:50.767 回答
5

处理程序连接AppDomain.UnhandledException为:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

但是我找不到在处理程序中标记为已处理的方法 - 所以无论你做什么,这似乎总是导致应用程序关闭。所以我不认为这很有用。

更好地处理Application.Current.DispatcherUnhandledException和测试CommunicationObjectFaultedException- 因为您可以通过重新初始化代理来从中恢复 - 就像您在初始连接时所做的那样。例如:

void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
    if (e.Exception is CommunicationObjectFaultedException) { //|| e.Exception is InvalidOperationException) {
        Reconnect();
        e.Handled = true;
    }
    else {
        MessageBox.Show(string.Format("An unexpected error has occured:\n{0}.\nThe application will close.", e.Exception));
        Application.Current.Shutdown();
    }
}

public bool Reconnect() {
    bool ok = false;
    MessageBoxResult result = MessageBox.Show("The connection to the server has been lost.  Try to reconnect?", "Connection lost", MessageBoxButton.YesNo);
    if (result == MessageBoxResult.Yes)
        ok = Initialize();
    if (!ok)
        Application.Current.Shutdown();
}

其中 Initialize 具有您的初始代理实例化/连接代码。

在您上面发布的代码中,我怀疑您正在处理DispatcherUnhandledException 两次- 通过在 xaml 和代码中连接处理程序。

于 2012-04-08T08:47:21.543 回答