Is there a way in the new async dotnet 4.5 library to set a timeout on the Task.WhenAll
method. I want to fetch several sources and stop after say 5 seconds and skip the sources that weren't finished.
You could combine the resulting Task
with a Task.Delay()
using Task.WhenAny()
:
await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(timeout));
If you want to harvest completed tasks in case of a timeout:
var completedResults =
tasks
.Where(t => t.Status == TaskStatus.RanToCompletion)
.Select(t => t.Result)
.ToList();