Await list of async predicates, but drop out on first false

后端 未结 6 1103
孤街浪徒
孤街浪徒 2021-01-12 21:20

Imagine the following class:

public class Checker
{
   public async Task Check() { ... }
}

Now, imagine a list of instances of

6条回答
  •  难免孤独
    2021-01-12 21:42

    As a more out-of-the-box alternative, this seems to run the tasks in parallel and return shortly after the first failure:

    var allResult = checkers
        .Select(c => Task.Factory.StartNew(() => c.Check().Result))
        .AsParallel()
        .All(t => t.Result);
    

    I'm not too hot on TPL and PLINQ so feel free to tell me what's wrong with this.

提交回复
热议问题