为什么调用SqlCommand.BeginExecuteNonQuery后我的任务状态是"WaitingForActivation"?

Why is my task in "WaitingForActivation" status after calling SqlCommand.BeginExecuteNonQuery?

给定以下代码:

           Dim t As Date = Now
            Dim e As New AsynchronousExecutionEventArgs
            AsyncDataExecuting = False
            Dim result As IAsyncResult = CType(Cmd, SqlClient.SqlCommand).BeginExecuteNonQuery()

            Do While Not (result.IsCompleted Or result.CompletedSynchronously)
                AsyncDataExecuting = True
                RaiseEvent OnAsynchronousProcessing(Me, e)

                If CInt(DateLib.TimeSpan(t, DateLib.TimeSpanTypeEnum.TicksPerMillisecond)) > AsynchronousProcessingDelay Then
                    _Common.InvokeDoEvents() 'we do this getting the datareader asynchronously so'
                End If
                If e.mCancel Then
                    If Not Cmd Is Nothing Then Cmd.Cancel()
                    If Not Cmd Is Nothing Then Cmd.Dispose()
                    AsyncDataExecuting = False
                    Exit Function
                End If
                System.Threading.Thread.Sleep(AsynchronousProcessingSleep)
            Loop
            If Not Cmd Is Nothing Then
                ExecSql = ToStr(CType(Cmd, SqlClient.SqlCommand).EndExecuteNonQuery(result))
            End If

我很困惑为什么有时结果对象是:

Id = 1, Status = WaitingForActivation {1}, Method = "{null}", Result = "{Not yet computed}" System.Threading.Tasks.Task(Of Object)

我原以为 BeginExecuteNonQuery 不允许任务处于 "WaitingForActivation" 模式。我如何 (1) 防止任务处于 WaitingForActiviation 模式或 (2) 激活它?

PS:奇怪的是,如果我设置了一个 SQL Profiler,我会看到 SQL 正在执行并且语句已成功执行,但代码陷入循环,因为"IsCompleted" 标志永远不会发送。

WaitingForActiviation表示此任务是基于TaskCompletionSource的,楼主还没有完成。我不明白这个名字是怎么来的

所以这是正常的。这意味着任务没有完成。我怀疑 SQL 查询是否真的完成并且 Task.IsCompleted 真的是错误的。如果您认为是这种情况post请提供更多详细信息以便找到错误。

为什么要使用旧的 APM 模式 (IAsyncResult)?这是过时的。使用 ExecuteNonQueryAsync.