多个并行与异步调用的await [英] multiple parallel async calls with await

查看:791
本文介绍了多个并行与异步调用的await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我知道,当运行时遇到下面的语句是包装了函数的其余部分作为一个回调(someCall()在这个例子中),这是异步调用的方法。在这种情况下anotherCall()将作为回调被执行以someCall():

as far as i know when runtime come 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();

我不知道是否有可能使运行时像这样进行:在异步调用的方式someCall()和immidatelly返回到调用线程,然后调用anotherCall()类似(无需等待someCall完成)。因为我需要这2种方法异步运行,并假设这些电话都只是发射后不管的电话。

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

所以是有可能只使用异步实现这个场景,并等待(不使用旧的开始/结束的机制)?

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

推荐答案

异步/的await包括一些运营商,以帮助并联组成,如 WhenAll WhenAny

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;

这篇关于多个并行与异步调用的await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆