当前位置: 首页 > 知识库问答 >
问题:

从 sub 到异步调用引发异常

关正雅
2023-03-14

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

这是由异步函数调用的常规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中运行时,它将在“抛出新异常(”在脉冲下创建车辆时出错“)处停止

这是调试器在那一行上所说的:

System.Exception类型的异常发生在MyProject.exe,但未在用户代码中处理。附加信息:在脉冲时创建车辆时出错……[被调用的subs抛出的错误消息向上传播]。算术操作导致溢出。

奇怪的是,在调试器中,如果我点击“单步执行”,它会返回到我的窗体中调用异步函数的sub,在GUI上显示消息框。

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

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

共有1个答案

顾宣
2023-03-14

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

调试器的行为有些奇怪,有点令人沮丧,但我仍然很高兴我的问题中列出的代码确实有效。

 类似资料:
  • 我可能还不够清楚--情况是,我的现有代码不支持异步,我希望使用新的库,如System.net.http和只支持异步方法的AWS SDK。因此,我需要弥补这一差距,并能够拥有可以同步调用的代码,然后可以在其他地方调用异步方法。 我读了很多书,有很多次有人问这个问题,也有人回答这个问题。 从非异步方法调用异步方法

  • 我有以下错误 ValidationException:HV000041:调用TraversableResolver.IsReachable()引发异常。在org.hibernate.validator.internal.engine.validatorimpl.isreachable(validatorimpl.java:1405)上 org.hibernate.validator.interna

  • 本文向大家介绍JavaScript 异步调用,包括了JavaScript 异步调用的使用技巧和注意事项,需要的朋友参考一下 问题 可修改下面的 aa() 函数,目的是在一抄后用 console.log() 输出 want-value 但是,有额外要求: aa() 函数可以随意修改,但是不能有 console.log() 执行 console.log() 语句里不能有 setTimeout 包裹 解

  • Provider端异步执行将阻塞的业务从Dubbo内部线程池切换到业务自定义线程,避免Dubbo线程池的过度占用,有助于避免不同服务间的互相影响。异步执行无益于节省资源或提升RPC响应性能,因为如果业务执行需要阻塞,则始终还是要有线程来负责执行。 注意:Provider端异步执行和Consumer端异步调用是相互独立的,你可以任意正交组合两端配置 Consumer同步 - Provider同步 C