Skip to main content
1 of 6
XDS
  • 4.3k
  • 4
  • 43
  • 71

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:

 var car = (Car) null; var cat = (Cat) null; var house = (House) null; using (var carTask = BuyCar()) using (var catTask = FeedCat()) using (var houseTask = SellHouse()) { try { await Task.WhenAll(carTask, catTask, houseTask); } finally { car = carTask.Status == TaskStatus.RanToCompletion ? await carTask : null; cat = catTask.Status == TaskStatus.RanToCompletion ? await catTask : null; house = houseTask.Status == TaskStatus.RanToCompletion ? await houseTask : null; } } 

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.

XDS
  • 4.3k
  • 4
  • 43
  • 71