gpt4 book ai didi

wpf - WPF 中非 UI 线程的全能异常处理程序

转载 作者:行者123 更新时间:2023-12-04 16:07:34 24 4
gpt4 key购买 nike

具体来说,我将 WPF 与 MVVM 一起使用。我有一个 MainWindow,它是一个 WPF 窗口,所有操作都在其中发生。它为其属性、命令等使用相应的 View 模型类。

我在 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

这部分有效

当我尝试通过故意抛出异常来测试它时,它可以工作。它实际上是在处理 Select All 按钮单击的 Sub 中的 View Model 中。

按钮:
<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 中还有另一个按钮,类似地绑定(bind)到一个命令。但是,它使用一个任务在后台执行它的工作,并且在那里抛出的异常不会被我的包罗万象的处理程序捕获。

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 事件调用我的事件处理程序。但是我从另一个答案中得到了提示,我将其标记为答案,因为它最终有所帮助。

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

我最终在 Task 中放置了一个 try-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

最佳答案

TaskScheduler.UnobservedTaskException 事件是你想从 App start 订阅的。
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 事件之前,有一些调用堆栈冒泡和正常异常操作的上下文切换的操作。
我想指出的一件事是,必须使用通用异常处理程序包装整个应用程序,以防止应用程序被终止。但是,请记住,对所有可能引发异常的路径实现适当的异常处理也是必须的。

关于wpf - WPF 中非 UI 线程的全能异常处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33089211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com