multiple parallel async calls with await

后端 未结 3 1561
忘了有多久
忘了有多久 2020-12-13 06:30

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

3条回答
  •  庸人自扰
    2020-12-13 07:33

    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.

提交回复
热议问题