WPF 中非 UI 线程的 Catch-all 异常处理程序

Catch-all Exception Handler for non-UI Threads in WPF

具体来说,我将 WPF 与 MVVM 结合使用。我有一个 MainWindow,它是一个 WPF Window,所有操作都在这里发生。它为其属性、命令等使用相应的视图模型class

我在 Application.xaml.vb StartUp 中设置了主 UI 线程和非 UI 线程异常处理程序,如下所示:

Private Sub Application_DispatcherUnhandledException(sender As Object, e As Windows.Threading.DispatcherUnhandledExceptionEventArgs) Handles Me.DispatcherUnhandledException
    ' catches main UI thread exceptions only
    ShowDebugOutput(e.Exception)
    e.Handled = True
End Sub

Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
    ' catches background exceptions
    Dim currentDomain As AppDomain = AppDomain.CurrentDomain
    AddHandler currentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
    AddHandler System.Threading.Tasks.TaskScheduler.UnobservedTaskException, AddressOf BackgroundTaskExceptionHandler
End Sub

Sub UnhandledExceptionHandler(sender As Object, args As UnhandledExceptionEventArgs)
    Dim ex As Exception = DirectCast(args.ExceptionObject, Exception)
    ShowDebugOutput(ex)
End Sub

Sub BackgroundTaskExceptionHandler(sender As Object, args As System.Threading.Tasks.UnobservedTaskExceptionEventArgs)
    Dim ex As Exception = DirectCast(args.Exception, Exception)
    ShowDebugOutput(ex)
End Sub

这部分有效

当我试图通过故意抛出异常来测试它时,它起作用了。它实际上是在 Sub 中的视图模型中处理 Select 所有按钮单击。

按钮:

<Button Content="Select All" Height="23" Width="110" Command="{Binding SelectAllCommand}" />

我抛出异常并成功捕获的命令:

Private Sub SelectAll()
    Throw (New Exception("UI Thread exception"))
    SetAllApplyFlags(True)
End Sub

这部分不工作

同一个 MainWindow 中还有另一个按钮类似地绑定到一个命令。但是,它使用 Task 在后台执行其工作,并且在那里抛出的异常不会被我的 catch-all 处理程序捕获。

Private Sub GeneratePreview()
    ' run in background
    System.Threading.Tasks.Task.Factory.StartNew(
        Sub()
            ' ... stuff snipped out, issue is the same with or without the rest of the code here ...
            Throw (New Exception("Throwing a background thread exception"))
        End Sub)
End Sub

有几个类似的问题,但我一直没能真正从中找出我的答案。 AppDomain UnhandledException 似乎是大多数情况下的答案,但它不适合我。我究竟需要添加什么才能捕获可能以这种方式在非 UI 线程中抛出的异常?

我最后做了什么

当我在 Application.xaml.vb 中处理它时,我无法让 TaskScheduler.UnobservedTaskException 事件调用我的事件处理程序。但我从另一个答案中得到了提示,我将其标记为答案,因为它最终有所帮助。

但是,它不在应用程序级别,所以如果这是一个更大的应用程序,我必须在我使用任务的每个实例中复制它。这不是我真正想要的,但现在不愿意花更多时间在这上面。

我最终在 任务中放置了一个 try-catch 。在 catch 中,我能够使用 Dispatcher.Invoke 仍然显示带有异常信息的用户友好对话框。

Private Sub GeneratePreview()
    ' run in background
    System.Threading.Tasks.Task.Factory.StartNew(
        Sub()
            Try
                ' ... stuff snipped out, issue is the same with or without the rest of the code here ...
                Throw (New Exception("Throwing a background thread exception"))
            Catch ex As Exception
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, DirectCast(
                    Sub()
                        HRNetToADImport.Application.ShowDebugOutput(ex)
                    End Sub, Action))
            End Try
        End Sub)
End Sub

桑德拉,

有一种方法可以从后台任务中捕获异常。
我承认我的解决方案不是全局的,但至少它可以捕获并防止崩溃!

Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim t As Task = Task.Factory.StartNew(
       Sub()
           ' ... stuff snipped out, issue is the same with or without the rest of the code here ...
           Throw (New Exception("Throwing a background thread exception"))
       End Sub)
    Try
        Await t
    Catch ex1 As Exception
        Debug.WriteLine("Exception from inside task " & ex1.Message)
    End Try
End Sub

认为它可能对您有帮助,可能对您有帮助,也可能对其他人有帮助。

TaskScheduler.UnobservedTaskException 事件就是你要从App开始订阅的。

https://msdn.microsoft.com/en-us/library/vstudio/system.threading.tasks.taskscheduler.unobservedtaskexception(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Occurs when a faulted Task's unobserved exception is about to trigger exception escalation policy, which, by default, would terminate the process.

This AppDomain-wide event provides a mechanism to prevent exception escalation policy (which, by default, terminates the process) from triggering.

注意:事件可能不会立即触发(可能会延迟几秒)。你可以想象在最终到达 UnobservedTaskException 事件之前,有一些调用堆栈冒泡和正常异常操作的上下文切换操作。

我想指出的一件事是,必须使用通用异常处理程序包装整个应用程序以防止应用程序被终止。但是,请记住,对所有可能抛出异常的路径实施适当的异常处理也是必须的。

桑德拉,

我阅读了cscmh99命题, 拿了你的代码,然后尝试 运行,它成功了!

我的意思是你可以订阅TaskScheduler.UnobservedTaskException

  • 然后你会抓到UnobservedException

  • 但是你不会捕获观察到的异常
    观察到的异常来自 .Wait().Result

  • 等待的任务
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    ' Here follows an Unobserved Exception

    System.Threading.Tasks.Task.Factory.StartNew(
        Sub()
            Throw (New Exception("Throwing a background thread exception"))
        End Sub)

    ' Here follows an ObservedException
    ' ObservedException because there is call to .Wait() (or .Result)
    ' You can't use UnobservedException for that one

    Dim t As Task = System.Threading.Tasks.Task.Factory.StartNew(
        Sub()
            Throw (New Exception("Throwing a background thread exception"))
        End Sub)
    t.Wait()
End Sub

这是工作解决方案的代码:http://1drv.ms/1XOvTbK

此致