Async Task.WhenAll with timeout

后端 未结 11 2123
梦谈多话
梦谈多话 2020-11-29 04:33

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

11条回答
  •  心在旅途
    2020-11-29 04:46

    Seems like the Task.WaitAll overload with the timeout parameter is all you need - if it returns true, then you know they all completed - otherwise, you can filter on IsCompleted.

    if (Task.WaitAll(tasks, myTimeout) == false)
    {
        tasks = tasks.Where(t => t.IsCompleted);
    }
    ...
    

提交回复
热议问题