应用程序在调用Application_DispatcherUnhandledException处理程序后崩溃

时间:2013-12-05 04:37:34

标签: wpf xaml exception-handling invalidoperationexception

在启动我的WPF应用程序时,我试着查看特定的必需exe是否正在运行。如果没有运行,我会像这样抛出InvalidOperationException

if (gaInterface.ConnectToGA() != 0)
            throw new InvalidOperationException(Properties.Resources.GAConnectionErrorText);

  private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        var exception = e.Exception;
        ShowErrorMessage(exception);
        if (string.CompareOrdinal(exception.Message, FP.Properties.Resources.GAConnectionErrorText) == 0 || (
            !ReferenceEquals(exception.InnerException, null) &&
            string.CompareOrdinal(exception.InnerException.Message, FP.Properties.Resources.GAConnectionErrorText) == 0))
            e.Handled = false;
    }

e.Handled确实设置为true,但仅在发布模式下应用程序崩溃。这可能是什么原因?

1 个答案:

答案 0 :(得分:1)

将e.Handled设置为true,然后关闭应用程序。

private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    var exception = e.Exception;
    ShowErrorMessage(exception);
    if (string.CompareOrdinal(exception.Message, FP.Properties.Resources.GAConnectionErrorText) == 0 || (
        !ReferenceEquals(exception.InnerException, null) &&
        string.CompareOrdinal(exception.InnerException.Message, FP.Properties.Resources.GAConnectionErrorText) == 0))
        {            
             e.Handled = true;
             Application.Current.Shutdown();
        }
}