在一个列表异步函数 [英] async function over a list

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

问题描述

我有一个看起来像这样的功能:

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:


  1. 开始所有的任务,然后的await 他们每个人。他们将所有并联运行,你的程序将继续,一旦他们都完成了。

  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.

把你的任务到一个集合,然后用等待 Task.WhenAll 等待多个任务。

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);

这篇关于在一个列表异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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