我将如何构造下面的代码,以便调用异步方法? Parallel.For(0, elevations.Count(), delegate(int i){ allSheets.AddRange(await BuildSheetsAsync(userID, elevations[i], includeLabels));}); Parallel.For()与异步方法不兼容.如
Parallel.For(0, elevations.Count(), delegate(int i) { allSheets.AddRange(await BuildSheetsAsync(userID, elevations[i], includeLabels)); });Parallel.For()与异步方法不兼容.如果您不需要限制并行度(即,您可以同时执行所有任务),则只需启动所有任务,然后等待它们完成:
var tasks = Enumerable.Range(0, elevations.Count()) .Select(i => BuildSheetsAsync(userID, elevations[i], includeLabels)); List<Bitmap> allSheets = (await Task.WhenAll(tasks)).SelectMany(x => x).ToList();