小编Aus*_*ALF的帖子

Task.Wait()不包含任何链接(ContinueWith)任务

如果你有

Private Shared Sub msg(Optional message As String = "")
    Debug.WriteLine(Threading.Thread.CurrentThread.ManagedThreadId & " " & message)
End Sub
<TestMethod()>
Public Sub TestMethod1()
    Dim cancel As New Threading.CancellationTokenSource
    msg("starting")
    Dim start As DateTime = DateTime.Now
    Dim task As New Task(Sub()
                             msg("in thread")
                             Do Until Now.Subtract(start).TotalMilliseconds > 2000
                                 Threading.Thread.Sleep(500)
                                 msg("loop")
                                 cancel.Token.ThrowIfCancellationRequested()
                             Loop
                         End Sub, cancel.Token, TaskCreationOptions.LongRunning)
    task.ContinueWith(Sub(result As task)
                          msg("Continue task running")
                          Debug.WriteLine(result.Status.ToString & If(result.Exception Is Nothing, ", no exception", ", exception occurred " & result.Exception.GetType.Name))
                          Do Until Now.Subtract(start).TotalMilliseconds > 10000
                              cancel.Token.ThrowIfCancellationRequested() …
Run Code Online (Sandbox Code Playgroud)

.net vb.net multithreading task task-parallel-library

1
推荐指数
1
解决办法
1020
查看次数