Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple awaits vs Task.WaitAll - equivalent?

In terms of performance, will these 2 methods run GetAllWidgets() and GetAllFoos() in parallel?

Is there any reason to use one over the other? There seems to be a lot happening behind the scenes with the compiler so I don't find it clear.

============= MethodA: Using multiple awaits ======================

public async Task<IHttpActionResult> MethodA() {     var customer = new Customer();      customer.Widgets = await _widgetService.GetAllWidgets();     customer.Foos = await _fooService.GetAllFoos();      return Ok(customer); } 

=============== MethodB: Using Task.WaitAll =====================

public async Task<IHttpActionResult> MethodB() {     var customer = new Customer();      var getAllWidgetsTask = _widgetService.GetAllWidgets();     var getAllFoosTask = _fooService.GetAllFos();      Task.WaitAll(new List[] {getAllWidgetsTask, getAllFoosTask});      customer.Widgets = getAllWidgetsTask.Result;     customer.Foos = getAllFoosTask.Result;      return Ok(customer); } 

=====================================

like image 296
vidalsasoon Avatar asked Aug 20 '15 13:08

vidalsasoon


People also ask

What is the difference between task WaitAll and task WhenAll?

The Task. WaitAll blocks the current thread until all other tasks have completed execution. The Task. WhenAll method is used to create a task that will complete if and only if all the other tasks have completed.

Can you tell difference between task WhenAll and task WhenAny?

WhenAll returns control after all tasks are completed, while WhenAny returns control as soon as a single task is completed.

What is task WaitAll?

WaitAll(Task[], TimeSpan) Waits for all of the provided cancellable Task objects to complete execution within a specified time interval. WaitAll(Task[], Int32, CancellationToken) Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled.

What is the use of task WhenAll?

WhenAll(Task[])Creates a task that will complete when all of the Task objects in an array have completed.


1 Answers

The first option will not execute the two operations concurrently. It will execute the first and await its completion, and only then the second.

The second option will execute both concurrently but will wait for them synchronously (i.e. while blocking a thread).

You shouldn't use both options since the first completes slower than the second and the second blocks a thread without need.

You should wait for both operations asynchronously with Task.WhenAll:

public async Task<IHttpActionResult> MethodB() {     var customer = new Customer();      var getAllWidgetsTask = _widgetService.GetAllWidgets();     var getAllFoosTask = _fooService.GetAllFos();      await Task.WhenAll(getAllWidgetsTask, getAllFoosTask);      customer.Widgets = await getAllWidgetsTask;     customer.Foos = await getAllFoosTask;      return Ok(customer); } 

Note that after Task.WhenAll completed both tasks already completed so awaiting them completes immediately.

like image 160
i3arnon Avatar answered Sep 22 '22 10:09

i3arnon