多个异步/等待链 [英] Multiple async/await chaining

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

问题描述

如何在C#中进行多个异步/等待链接? 例如,启动几个HTTP请求,然后不等待所有请求,而是在每个请求完成后启动新请求?

How to make multiple async/await chaining in C#? For example, start few HTTP requests and then do not wait all of them, but start new request after each completed?

推荐答案

最简单的方法是编写async方法:

The easiest way to do this is to write an async method:

async Task DownloadAndFollowupAsync(...)
{
  await DownloadAsync();
  await FollowupAsync();
}

然后可以与await Task.WhenAll一起使用:

await Task.WhenAll(DownloadAndFollowupAsync(...),
    DownloadAndFollowupAsync(...));

这篇关于多个异步/等待链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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