Skip to main content
added 1137 characters in body
Source Link
XDS
  • 4.3k
  • 4
  • 43
  • 71

An alternative implementation that has more or less the same performance characteristics could be:

 public async Task<string> Foobar() { using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { cat = catTask.Status == TaskStatus.RanToCompletion ? catTask.Result : (await catTask); car = carTask.Status == TaskStatus.RanToCompletion ? carTask.Result : (await carTask); house = houseTask.Status == TaskStatus.RanToCompletion ? houseTask.Result : (await houseTask); return DoSomething(cat, car, house); } } 

An alternative implementation that has more or less the same performance characteristics could be:

 public async Task<string> Foobar() { using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { cat = catTask.Status == TaskStatus.RanToCompletion ? catTask.Result : (await catTask); car = carTask.Status == TaskStatus.RanToCompletion ? carTask.Result : (await carTask); house = houseTask.Status == TaskStatus.RanToCompletion ? houseTask.Result : (await houseTask); return DoSomething(cat, car, house); } } 
deleted 160 characters in body
Source Link
XDS
  • 4.3k
  • 4
  • 43
  • 71

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:

 public async Task<string> Foobar() { async Task<string> Awaited(Task<Cat> a, Task<House> b, Task<Tesla> c) { return DoSomething(await a, await b, await c); } using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { if (carTask.Status == TaskStatus.RanToCompletion //triple && catTask.Status == TaskStatus.RanToCompletion //cache && houseTask.Status == TaskStatus.RanToCompletion) { //hits return Task.FromResult(DoSomething(catTask.Result, carTask.Result, houseTask.Result)); //fast-track } cat = await catTask; car = await carTask; house = await houseTask; //or Task.AwaitAll(carTask, catTask, houseTask); //or await Task.WhenAll(carTask, catTask, houseTask); //it depends on how you like exception handling better 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.

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:

 public async Task<string> Foobar() { async Task<string> Awaited(Task<Cat> a, Task<House> b, Task<Tesla> c) { return DoSomething(await a, await b, await c); } using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { if (carTask.Status == TaskStatus.RanToCompletion //triple && catTask.Status == TaskStatus.RanToCompletion //cache && houseTask.Status == TaskStatus.RanToCompletion) { //hits return Task.FromResult(DoSomething(catTask.Result, carTask.Result, houseTask.Result)); //fast-track } cat = await catTask; car = await carTask; house = await houseTask; //or Task.AwaitAll(carTask, catTask, houseTask); //or await Task.WhenAll(carTask, catTask, houseTask); //it depends on how you like exception handling better 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.

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:

 public async Task<string> Foobar() { async Task<string> Awaited(Task<Cat> a, Task<House> b, Task<Tesla> c) { return DoSomething(await a, await b, await c); } using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { if (carTask.Status == TaskStatus.RanToCompletion //triple && catTask.Status == TaskStatus.RanToCompletion //cache && houseTask.Status == TaskStatus.RanToCompletion) { //hits return Task.FromResult(DoSomething(catTask.Result, carTask.Result, houseTask.Result)); //fast-track } cat = await catTask; car = await carTask; house = await houseTask; //or Task.AwaitAll(carTask, catTask, houseTask); //or await Task.WhenAll(carTask, catTask, houseTask); //it depends on how you like exception handling better return Awaited(catTask, carTask, houseTask); } } 
deleted 135 characters in body
Source Link
XDS
  • 4.3k
  • 4
  • 43
  • 71

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.

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:

  var car = (Car) null; var cat = (Cat) null; var house = (House) null; using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { try {  await Task.WhenAll(carTask, catTask, houseTask); //or Task.WhenAny(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;  }  if (cat == null || car == null || house == null)  throw new SomethingFishyGoingOnHereException("..."); } 

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.

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:

 public async Task<string> Foobar() { async Task<string> Awaited(Task<Cat> a, Task<House> b, Task<Tesla> c) {   return DoSomething(await a, await b, await c);   }   using (var carTask = BuyCarAsync()) using (var catTask = FeedCatAsync()) using (var houseTask = SellHouseAsync()) { if (carTask.Status == TaskStatus.RanToCompletion //triple  && catTask.Status == TaskStatus.RanToCompletion //cache  && houseTask.Status == TaskStatus.RanToCompletion) { //hits  return Task.FromResult(DoSomething(catTask.Result, carTask.Result, houseTask.Result)); //fast-track   }  cat = await catTask;  car = await carTask;  house = await houseTask;  //or Task.AwaitAll(carTask, catTask, houseTask);  //or await Task.WhenAll(carTask, catTask, houseTask); //it depends on how you like exception handling better      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.

added 48 characters in body
Source Link
XDS
  • 4.3k
  • 4
  • 43
  • 71
Loading
add a warning about entityframework
Source Link
XDS
  • 4.3k
  • 4
  • 43
  • 71
Loading
Source Link
XDS
  • 4.3k
  • 4
  • 43
  • 71
Loading