为了稳健性,我想为 TaskScheduler.UnobservedTaskException 添加一个处理程序。处理程序确实记录异常通知用户并关闭应用程序。
下面的实现有意义吗?在 WPF 中是否可以在此处理程序中显示 MessageBox 或 Windows,或者这是一个坏主意,因为我们在未观察任务的终结器中运行?
Private Sub TaskSheduler_UnobservedTaskException(sender As Object, e As UnobservedTaskExceptionEventArgs)
Static _wasUserInformed As Boolean = False
e.SetObserved()
'Trace all Exceptions
e.Exception.Flatten.Handle(Function(ex As Exception)
'TODO trace and log Exception
Debug.Print("UnobservedTaskException: {0}", ex.Message)
Return True
End Function)
If Not _wasUserInformed Then
'Show root Exception
_wasUserInformed = True
Application.Current.Dispatcher.BeginInvoke(Sub()
'MessageBox.Show(e.Exception.GetBaseException.Message)
Dim win As New UnexpectedExceptionWindow
win.UnexpectedException = e.Exception.GetBaseException
win.ShowDialog()
Application.Current.Dispatcher.BeginInvoke(Sub() Application.Current.Shutdown())
End Sub)
End If
结束子
[编辑] 作为我们讨论的结果,我想出了以下解决方案。
Private Sub TaskScheduler_UnobservedTaskException(sender As Object, e As UnobservedTaskExceptionEventArgs
) Handles TaskScheduler.UnobservedTaskException
Static _wasUserInformed As Boolean = False
'Free the finalizer thread and execute on the UI thread to be able to inform user
Dispatcher.BeginInvoke(Sub() LogException(e.Exception))
e.SetObserved()
If Not _wasUserInformed Then
_wasUserInformed = True
'Show first error
Dispatcher.BeginInvoke(Sub()
NotifyUser(e.Exception)
Application.Current.Shutdown()
End Sub)
End If
End Sub