从子线程到异步调用抛出异常



这是一个windows窗体应用程序,我在其中有一个特定的窗体。在这个表单上,我显示了一些处理的进度,这些处理应该在后台异步发生。所有的工作都很好,除了当我试图处理在后台处理中捕获的异常....

这是在我的表单的代码调用Async函数的子,这是在一个模块中包含所有的后台处理代码:

Public Async Sub BasicProcessing()
        Try
            Dim processTarget As Action(Of Integer)
            processTarget = AddressOf UpdatePulseProcessing
            myProgress = New Progress(Of Integer)(processTarget)
            myCount.Vehicles = Await ProcessmyCountFile(myCount, myProgress)
            If OperationCanceledByUser = True Then
                Exit Sub
            End If
    Catch ex As Exception
        MessageBox.Show(Me, "Unable to update count." _
                        & Environment.NewLine & ex.Message, _
                        "Error updating count", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End Try
End Sub

这是它调用的async函数,位于单独的模块中:

Public Function ProcessmyCountFile(CountToProcess As Count, ByVal ProgressObject As IProgress(Of Integer)) As Task(Of List(Of Vehicle))
    myProgressObject = ProgressObject
    basicToken = New CancellationTokenSource
    Try
        Return CType(Task(Of List(Of Vehicle)).Run(Function()
                                                       If basicToken.IsCancellationRequested Then
                                                           Return Nothing
                                                           Exit Function
                                                       End If
                                                       myCountFile = CountToProcess
                                                       MyVehicles = New List(Of Vehicle)
                                                       'All that is important in here to note is a call to a regular sub within this module
                                                       CreateVehicles()
                                                       Return MyVehicles
                                                   End Function, basicToken.Token), Global.System.Threading.Tasks.Task(Of List(Of Global.STARneXt.Vehicle)))
    Catch ex As Exception
        Throw New Exception(ex.Message)
        Return Nothing
    End Try
End Function
Public Sub StopProcess()
    If Not basicToken Is Nothing Then
        basicToken.Cancel() ' We tell our token to cancel
    End If
End Sub

这是Async函数调用的常规子对象:

Private Sub CreateVehicles()
    Try
        'In here are calls to other regular subs within the same module, let's just call them A and B
    Catch ex As Exception
        StopProcess()
        Throw New Exception("Error creating vehicles at pulse " & pulsePointer & ". " & ex.Message)
    End Try
End Sub

当我使用我知道的数据运行此代码时,最终会在子B中生成错误,错误确实向上传播,直到由异步函数....直接调用的方法因此,当在VS中运行时,它将停止在"抛出新的异常"("错误创建车辆在脉冲"&pulsePointer,"。",ex.Message)",其中Message包含子b抛出的消息。

这是调试器在那行显示的内容:

类型为'System '的异常。在MyProject.exe中发生异常未在用户代码中处理。附加信息:创建错误脉冲车辆....[错误消息向上传播,被调用潜艇)。算术运算导致溢出。

然后奇怪的是,在调试器中,如果我点击"Step Into",它就会返回到我的表单中调用Async函数的子,该函数在GUI上显示消息框。

那么我如何让它自动返回到原始表单代码来显示消息框呢?为什么它停在原地而不继续传播?

仅供参考,我确实发现了这个问题,但它最终没有帮助。

@StephenCleary是正确的-我为我的项目创建了一个新的安装,并且在安装的版本中,我确实得到了包含预期错误消息的消息框。

调试器的奇怪行为,有点令人沮丧,但尽管如此,我还是很高兴我的问题中列出的代码确实可以正常工作。

最新更新