我有一个简单的问题 - 为什么异步方法不等待并行循环完成呢?
public async Task<List<object>> DoSomeAsync()
{
// some async actions with streams and web requests
// ...
ConcurrentQueue<object> queue = new ConcurrentQueue<object>();
Parallel.For(1, x, async i =>
{
// a little chunk of code
// ...
queue.Enqueue(new object());
// ...
// a little chunk of code again
}
return queue.ToList(); // debugger says that this statement is executed earlier than parallel loop.
}
你知道如何等待并行循环执行的方法吗?
await Parallel.For(...)
。 - GrundyParallel.For
循环内部是否有任何可等待的方法? - George Chondrompilas