我有 ...
Private Sub TestTask()
Debug.Write("Running")
For i As Integer = 0 To 60
Debug.Write(".")
System.Threading.Thread.Sleep(1000)
Next
Debug.WriteLine("Finished")
End Sub
……
Dim cts As New CancellationTokenSource
Dim oToken As CancellationToken = cts.Token
'Create HelperTask to wait for cancellation request
Dim oHelperTask As Task = Task.Factory.StartNew(Function()
'Create Task to invoke function
Dim oTask As Task = Task.Factory.StartNew(Function()
Return outerFunction.Invoke
End Function, oToken)
' wait for cancellation token if Task is not complete
While oTask.Status = TaskStatus.Running
Thread.Sleep(200)
If oToken.IsCancellationRequested Then
oToken.ThrowIfCancellationRequested()
Return Nothing
End If
End While
Return oTask.Result
End Function, oToken)
cts.cancel()
但是在我的 Visual sudio 调试窗口中,我的 TestTask() 继续运行.....请任何人赐教。谢谢