Is it possible to get successful results from a Task.WhenAll when one of the tasks fails?

后端 未结 4 651
陌清茗
陌清茗 2021-01-03 10:22

Given the following:

var tPass1 = Task.FromResult(1);
var tFail1 = Task.FromException(new ArgumentException(\"fail1\"));
var tFail2 = Task.FromExc         


        
4条回答
  •  有刺的猬
    2021-01-03 10:29

    Change

    var task = Task.WhenAll(tPass1, tFail1, tFail2);
    task.Wait();
    

    to

    var all = new Task[] { tPass1, tFail1, tFail2 }
        .Where(t => t.Status == TaskStatus.RanToCompletion);
    var task = Task.WhenAll(all);
    task.Wait();
    

    Working example

提交回复
热议问题