multiple parallel async calls with await

后端 未结 3 1559
忘了有多久
忘了有多久 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条回答
  •  Happy的楠姐
    2020-12-13 07:19

    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;
    

提交回复
热议问题