【问题标题】:C# WPF Dispatcher.UnhandledException fails to catch exceptionC# WPF Dispatcher.UnhandledException 未能捕获异常
【发布时间】:2018-11-26 19:17:37
【问题描述】:

继承的 WPF 应用程序在 App.xaml.cs 中设置 Dispatcher.UnhandledException:

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

除了我偶尔会在没有调用 OnDispatcherUnhandledException 的情况下得到“程序已停止工作”。 根据其他线程中的大量讨论,我添加以下内容:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

但不明白这有什么帮助。 谁能解释一下 Dispatcher 和 CurrentDomain UnhandledException 的区别?

【问题讨论】:

    标签: c# wpf unhandled-exception


    【解决方案1】:

    看起来您在另一个线程中发生了异常。您可以调试 CurrentDomain_UnhandledException 以了解它们的来源。 Dispatcher.UnhandledException 处理与该 Dispatcher 关联的特定 UI 线程上的异常。如果您只有一个 UI 线程(通常),它应该处理这些异常。如果您在其他线程(很可能是非 UI 线程)中有异常,则可以使用 AppDomain.CurrentDomain.UnhandledException 处理这些异常。

    http://blogs.microsoft.co.il/arik/2011/08/28/order-in-chaos-handling-unhandled-exceptions-in-a-wpf-application/

    【讨论】:

    • 由于我还没有堆栈跟踪,我不知道异常的来源。但我想这是一个合理的推断,它不在 UI 线程中?因此实施 AppDomain.CurrentDomain.UnhandledException 可能会抓住它?谢谢。
    【解决方案2】:

    您可能会遇到少数无法捕获的异常。例如。 StackOverflowExceptionThreadAbortExceptionAccessViolationException(通常来自错误的本机代码)。

    阅读this answer了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多