使用异步/等待多个任务 [英] Using async/await for multiple tasks

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

问题描述

我使用的API客户端是完全 - 异步,即每个操作或者返回工作任务< T> ,例如:

 静态异步任务DoSomething的(INT SITEID,诠释postId,IBlogClient客户端)
{
    等待client.DeletePost(SITEID,postId); //调用API客户端
    Console.WriteLine(已删除后{0},SITEID);
}
 

使用C#5异步/等待运营商,什么是启动多个任务,等待他们所有正确的/最有效的方式来完成:

  INT [] IDS =新的[] {1,2,3,4,5};
Parallel.ForEach(IDS,I => DoSomething的(1,我,blogClient).Wait());
 

  INT [] IDS =新的[] {1,2,3,4,5};
Task.WaitAll(ids.Select(I => DoSomething的(1,我,blogClient))的ToArray());
 

由于API客户端使用内部HttpClient的,我希望它可以立即发出5 HTTP请求,写入控制台,每个人完成。

解决方案

  INT [] IDS =新的[] {1,2,3,4,5};
Parallel.ForEach(IDS,I => DoSomething的(1,我,blogClient).Wait());
 

虽然上述code运行并行操作,这code块每一个操作运行在每个线程。例如,如果网络电话需要2秒,则每个线程挂起2秒W / O做任何事情,但等待。

  INT [] IDS =新的[] {1,2,3,4,5};
Task.WaitAll(ids.Select(I => DoSomething的(1,我,blogClient))的ToArray());
 

在另一方面,上述code与为WaitAll 也阻止了线程和你的线程不会随意处理任何其他工作,直到操作结束。我想preFER WhenAll 这将异步并行执行的操作。

 公共异步任务的DoWork(){

    INT [] IDS =新[] {1,2,3,4,5};
    等待Task.WhenAll(ids.Select(I => DoSomething的(1,我,blogClient)));
}
 

要支持这一行动,这里是一个详细的博客文章通过所有的 选择和自己的优势/劣势:<一href="http://www.tugberkugurlu.com/archive/how-and-where-concurrent-asynchronous-io-with-asp-net-web-api">How而当并发异步I / O使用的ASP.NET Web API

I'm using an API client that is completely asynchrounous, that is, each operation either returns Task or Task<T>, e.g:

static async Task DoSomething(int siteId, int postId, IBlogClient client)
{
    await client.DeletePost(siteId, postId); // call API client
    Console.WriteLine("Deleted post {0}.", siteId);
}

Using the C# 5 async/await operators, what is the correct/most efficient way to start multiple tasks and wait for them all to complete:

int[] ids = new[] { 1, 2, 3, 4, 5 };
Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait());

or:

int[] ids = new[] { 1, 2, 3, 4, 5 };
Task.WaitAll(ids.Select(i => DoSomething(1, i, blogClient)).ToArray());

Since the API client is using HttpClient internally, I would expect this to issue 5 HTTP requests immediately, writing to the console as each one completes.

解决方案

int[] ids = new[] { 1, 2, 3, 4, 5 };
Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait());

Although you run the operations in parallel with the above code, this code blocks each thread that each operation runs on. For example, if the network call takes 2 seconds, each thread hangs for 2 seconds w/o doing anything but waiting.

int[] ids = new[] { 1, 2, 3, 4, 5 };
Task.WaitAll(ids.Select(i => DoSomething(1, i, blogClient)).ToArray());

On the other hand, the above code with WaitAll also blocks the threads and your threads won't be free to process any other work till the operation ends. I would prefer WhenAll which will perform your operations asynchronously in Parallel.

public async Task DoWork() {

    int[] ids = new[] { 1, 2, 3, 4, 5 };
    await Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient)));
}

To back this up, here is a detailed blog post going through all the alternatives and their advantages/disadvantages: How and Where Concurrent Asynchronous I/O with ASP.NET Web API

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

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