multiple parallel async calls with await
Asked Answered
S

3

40

As far as I know, when runtime comes across the statement below it wraps the rest of the function as a callback to the method which is invoked asynchronously (someCall() in this example). In this case anotherCall() will be executed as a callback to someCall():

    await someCall();
    await anotherCall();

I wonder if it is possible to make runtime perform like this: call someCall() in async fashion and return immediately to the calling thread, then invoke anotherCall() similarly (without waiting someCall to complete). Because I need these two methods to run asynchronously and suppose these calls are just fire and forget calls.

Is it possible to implement this scenario using just async and await (not using old begin/end mechanism)?

Shaker answered 12/5, 2011 at 7:21 Comment(0)
I
54

The async/await includes a few operators to help with parallel composition, such as WhenAll and WhenAny.

var taskA = someCall(); // Note: no await
var taskB = anotherCall(); // Note: no await

// Wait for both tasks to complete.
await Task.WhenAll(taskA, taskB);

// Retrieve the results.
var resultA = taskA.Result;
var resultB = taskB.Result;
Irwin answered 30/8, 2011 at 13:58 Comment(2)
Is there any of this for EcmaScript / Javascript?Ferocious
@Azteca: Sure, you can use Promise.all in place of Task.WhenAll, or Promise.race in place of Task.WhenAny. E.g., await Promise.all([promiseA, promiseB]);Irwin
H
7

The simplest way is probably to do this:

var taskA = someCall();
var taskB = someOtherCall();
await taskA;
await taskB;

This is especially nice if you want the result values:

var result = await taskA + await taskB;

so you don't need to do taskA.Result.

TaskEx.WhenAll might be faster than two awaits after each other. i don't know since I haven't done performance investigation on that, but unless you see a problem I think the two consecutive awaits reads better, especially if you ewant the result values.

Harriet answered 18/1, 2012 at 20:33 Comment(2)
Quoting "Async in C# 5.0": "This is a dangerous way to await multiple Tasks, if they may throw exceptions. If both operations throw an exception, the first await will propagate its exception, which means taskB is never awaited. Its exception will not be observed, and depending on .NET version and settings, may be lost or even rethrown on an unexpected thread, terminating the process."Decoder
So you'd better stick to await Task.WhenAll(taskA, taskB);Decoder
F
2

The Async CTP is no longer needed provided you're using .NET 4.5. Note that the async functionality is implemented by the compiler so .NET 4 apps can use it but VS2012 is required to compile it.

TaskEx is not needed anymore. The CTP couldn't modify the existing framework so it used extensions to accomplish things that the language would handle in 5.0. Just use Task directly.

So herewith, I have re-written the code(answered by Stephen Cleary) by replacing TaskEx with Task.

var taskA = someCall(); // Note: no await
var taskB = anotherCall(); // Note: no await

// Wait for both tasks to complete.
await Task.WhenAll(taskA, taskB);

// Retrieve the results.
var resultA = taskA.Result;
var resultB = taskB.Result;
Fitzpatrick answered 26/3, 2014 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.