Forward Warning
Just a quick headsup to those visiting this and other similar threads looking for a way to parallelize EntityFramework using async+await+task tool-set: The pattern shown here is sound, however, when it comes to the special snowflake of EF you will not achieve parallel execution unless and until you use a separate (new) db-context-instance inside each and every *Async() call involved.
This sort of thing is necessary due to inherent design limitations of ef-db-contexts which forbid running multiple queries in parallel in the same ef-db-context instance.
Capitalizing on the answers already given, this is the way to make sure that you collect all values even in the case that one or more of the tasks results in an exception:
varpublic carasync =Task<string> Foobar(Car) null;{ var catasync =Task<string> Awaited(CatTask<Cat> a, Task<House> b, Task<Tesla> c) null;{ var house = return DoSomething(Houseawait a, await b, await c); null; } using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { tryif { (carTask.Status == TaskStatus.RanToCompletion //triple await Task.WhenAll(carTask,&& catTask,.Status houseTask);== TaskStatus.RanToCompletion //orcache Task.WhenAny(carTask, catTask, houseTask); } finally&& { houseTask.Status == TaskStatus.RanToCompletion) { //hits carreturn =Task.FromResult(DoSomething(catTask.Result, carTask.Status ==Result, TaskStatushouseTask.RanToCompletionResult)); ?//fast-track await carTask : null; } cat = await catTask; cat = catTask.Status == TaskStatus.RanToCompletion ?car = await catTaskcarTask; : null; house = await houseTask; house = houseTask.Status ==//or TaskStatusTask.RanToCompletion ?AwaitAll(carTask, awaitcatTask, houseTask); : null; //or await } Task.WhenAll(carTask, catTask, houseTask); if//it (catdepends ==on nullhow ||you carlike ==exception nullhandling ||better house == null) throw new SomethingFishyGoingOnHereException return Awaited("..."catTask, carTask, houseTask); } }
The aggregate exception which contains one or more sub-exceptions will still be thrown at the end. It's up to the calling environment to handle that properly.