同时启动多个异步/等待功能,并分别处理它们 [英] Starting multiple async/await functions at once and handling them separately

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

问题描述

如何启动一次多个 HttpClient.GetAsync()请求,并尽快各自的反应回来处理他们每个?首先是我的尝试是:

  VAR response1 =等待client.GetAsync(http://example.com/);
VAR响应2 =等待client.GetAsync(http://stackoverflow.com/);
HandleExample(response1);
HandleStackoverflow(响应2);

但是,当然,它仍然是连续的。于是我试图立刻开始他们两个:

  VAR任务1 = client.GetAsync(http://example.com/);
VAR TASK2 = client.GetAsync(http://stackoverflow.com/);
HandleExample(等待独立写作);
HandleStackoverflow(等待TASK2);

现在的任务的启动的在同一时间,这是很好的,但当然code仍然要等待一前一后。

我要的是能够尽快处理example.com响应,它有,并尽快,因为它的用武之地。在stackoverflow.com回应

我可以把两个任务数组中使用 Task.WaitAny()在一个循环,检查哪一个成品,并调用相应的处理程序,但后来.. 。这怎么不只是普通的旧回调更好?或者这是不是一个真正的用途情况下异步/的await?如果没有,我怎么会用 HttpClient.GetAsync()与回调?

要澄清 - 我追求的行为是这样的伪code:

  client.GetAsyncWithCallback(http://example.com/,HandleExample);
client.GetAsyncWithCallback(http://stackoverflow.com/,HandleStackoverflow);


解决方案

您可以使用 ContinueWith WhenAll 来等待一种新的工作,任务1和Task2将并行执行

  VAR任务1 = client.GetAsync(http://example.com/)
                  .ContinueWith(T => HandleExample(t.Result));VAR TASK2 = client.GetAsync(http://stackoverflow.com/)
                  .ContinueWith(T => HandleStackoverflow(t.Result));VAR的结果=等待Task.WhenAll(新[] {独立写作,TASK2});

How do you start multiple HttpClient.GetAsync() requests at once, and handle them each as soon as their respective responses come back? First what I tried is:

var response1 = await client.GetAsync("http://example.com/");
var response2 = await client.GetAsync("http://stackoverflow.com/");
HandleExample(response1);
HandleStackoverflow(response2);

But of course it's still sequential. So then I tried starting them both at once:

var task1 = client.GetAsync("http://example.com/");
var task2 = client.GetAsync("http://stackoverflow.com/");
HandleExample(await task1);
HandleStackoverflow(await task2);

Now the tasks are started at the same time, which is good, but of course the code still has to wait for one after the other.

What I want is to be able to handle the "example.com" response as soon as it comes in, and the "stackoverflow.com" response as soon as it comes in.

I could put the two tasks in an array an use Task.WaitAny() in a loop, checking which one finished and call the appropriate handler, but then ... how is that better than just regular old callbacks? Or is this not really an intended use case for async/await? If not, how would I use HttpClient.GetAsync() with callbacks?

To clarify -- the behaviour I'm after is something like this pseudo-code:

client.GetAsyncWithCallback("http://example.com/", HandleExample);
client.GetAsyncWithCallback("http://stackoverflow.com/", HandleStackoverflow);

解决方案

You can use ContinueWith and WhenAll to await one new Task, task1 and task2 will be executed in parallel

var task1 = client.GetAsync("http://example.com/")
                  .ContinueWith(t => HandleExample(t.Result));

var task2 = client.GetAsync("http://stackoverflow.com/")
                  .ContinueWith(t => HandleStackoverflow(t.Result));

var results = await Task.WhenAll(new[] { task1, task2 });

这篇关于同时启动多个异步/等待功能,并分别处理它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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