从sub到异步调用抛出异常

时间:2016-08-16 19:46:16

标签: vb.net exception asynchronous

这是一个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

这是它调用的异步函数,它位于一个单独的模块中:

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

这是异步函数调用的常规子:

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提出的消息。 / p>

这是调试器在该行上所说的内容:

  

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

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

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

仅供参考,我确实找到this question,但最终没有帮助。

1 个答案:

答案 0 :(得分:0)

@StephenCleary是对的 - 我为我的项目创建了一个新安装,在安装的版本中,我确实收到了带有预期错误消息的消息框。

调试器有一些奇怪的行为,有点令人沮丧,但不过我很高兴我的问题中的代码确实有用。

相关问题