async function over a list

女生的网名这么多〃 提交于 2019-12-10 13:47:56

问题


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?


回答1:


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:

  1. 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.

  2. Put your tasks into a collection and then use awaitTask.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);


来源:https://stackoverflow.com/questions/11783479/async-function-over-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!