How to use await in a parallel foreach?

前端 未结 1 359
星月不相逢
星月不相逢 2021-01-12 05:31

So I sepnt the better part of the night trying to figure this out.

I was fortunate to get introduced to the parallel.foreach yesterday and it works like I want it t

1条回答
  •  孤街浪徒
    2021-01-12 06:19

    Don't use Parralel.ForEach at all. Make your method to return Task instead of void, collect all the task and wait them like:

    Task.WaitAll(data.Select(d => MyMethod(d, someParam)).ToArray());
    

    0 讨论(0)
提交回复
热议问题