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
More elegant from my opinion :
await Task.Run(()=> Task.WaitAll(myArrayOfTasks), theCancellationToken);
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.
You can also await a delay:
await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(1000));
Use TaskCompletionSource<T>
to create a task for some asynchronous condition that does not already have an asynchronous API. Use CancellationToken.Register
to hook the modern CancellationToken-based cancellation system into another cancellation system. Your solution just needs to combine these two.
I have a CancellationToken.AsTask() extension method in my AsyncEx library, but you can write your own as such:
<System.Runtime.CompilerServices.Extension> _
Public Shared Function AsTask(cancellationToken As CancellationToken) As Task
Dim tcs = New TaskCompletionSource(Of Object)()
cancellationToken.Register(Function() tcs.TrySetCanceled(), useSynchronizationContext := False)
Return tcs.Task
End Function
Usage is as you expected:
Await Task.WhenAny(Task.WhenAll(taskCollection), cancellationToken.AsTask())