【问题标题】:Application crashes after Application_DispatcherUnhandledException handler called调用 Application_DispatcherUnhandledException 处理程序后应用程序崩溃
【发布时间】:2013-12-05 04:37:33
【问题描述】:

在启动我的 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,但仅在发布模式下应用程序在此之后崩溃。这可能是什么原因?

【问题讨论】:

  • 您还期待什么?你让异常通过 throw,难道你不能捕获异常并做你的工作吗?
  • 我在代码中看不到“e.Handled 确实设置为 true”的行。

标签: wpf xaml exception-handling invalidoperationexception


【解决方案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();
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多