How can I cancel Task.WhenAll?

前端 未结 4 974
执笔经年
执笔经年 2020-12-10 11:20

Currenly using the following code to wait for a collection of tasks to complete. However, I now have a situation where I want to be able to cancel/abort the WhenAll call, v

4条回答
  •  余生分开走
    2020-12-10 11:38

    Dim tcs as new TaskCompletionSource(Of Object)()
    Await Tasks.Task.WhenAny(Tasks.Task.WhenAll(TaskCollection), tcs)
    

    To cancel, call tcs.SetResult(Nothing). This will fire your Task.WhenAny.

提交回复
热议问题