在一个列表异步函数 [英] async function over a list
问题描述
我有一个看起来像这样的功能:
I Have a function that looks like this:
public async Task<decimal> GoToWeb(string Sym){}
什么叫过来字符串列表的最佳方式?
what's the best way to call it over a list of strings?
推荐答案
的这里的从MSDN使用异步等待
来并行处理任务multilpe的文章。和这里的另一个专门针对一个收集任务。
Here's an article from MSDN on using async-await
to process multilpe tasks in parallel. And here's another that specifically addresses a collection of tasks.
在短,您可以执行下列操作之一:
In short, you can do one of the following:
-
开始所有的任务,然后
的await
他们每个人。他们将所有并联运行,你的程序将继续,一旦他们都完成了。
Start all of your tasks and then
await
each of them. They will all run in parallel and your program will continue once they all complete.
把你的任务到一个集合,然后用等待
的 Task.WhenAll
等待多个任务。
Put your tasks into a collection and then use await
Task.WhenAll
to wait for multiple tasks.
第二方法的一个例子是如下:
An example of the second method would be as follows:
List<string> Syms = ... // Create your list of strings
IEnumerable<Task<decimal>> tasks = from Sym in Syms select GoToWeb(Sym);
decimal[] results = await Task.WhenAll(tasks);
这篇关于在一个列表异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!