【发布时间】:2016-04-06 18:34:47
【问题描述】:
MSDN 示例(回复:https://msdn.microsoft.com/en-us/library/hh556530.aspx)演示了这种使用 Async/Await 构造和 LINQ 对象库运行多个异步 I/O 下载操作的技术,如以下代码 sn-p 所示:
// Create a query.
IEnumerable<Task<int>> downloadTasksQuery =
from url in urlList select ProcessURLAsync(url);
// Use ToArray to execute the query and start the download tasks.
Task<int>[] downloadTasks = downloadTasksQuery.ToArray();
int[] lengths = await Task.WhenAll(downloadTasks);
async Task<int> ProcessURL(string url, HttpClient client)
{
byte[] byteArray = await client.GetByteArrayAsync(url);
DisplayResults(url, byteArray);
return byteArray.Length;
}
我正在寻找一种简单的替代方法,可以不使用 LINQ 来实现相同的功能,即将这两行代码替换为一些核心 Generics/Array C# 方法:
IEnumerable<Task<int>> downloadTasksQuery = from url in urlList select ProcessURLAsync(url);
Task<int>[] downloadTasks = downloadTasksQuery.ToArray();
【问题讨论】:
标签: c# arrays async-await task-parallel-library ienumerable