Say I want to make parallel API post requests.
In a for loop I can append the http post call into a list of tasks, (each task invoked using Task.Run) and then wait for all to finish using await Task.WhenAll. Thus the control will go to caller while waiting for the network request to complete. Effectively the API request will be made in parallel.
Similarly I can use Parallel.ForEachAsync which will automatically do the WhenAll and return control to caller. So I want to ask whether ForEachAsync is a replacement to a plain for loop list (async await Task.Run) and WhenAll?
Parallel.ForEachdoes a lot more than just use multiple tasks. - it partitions the data so that each worker task won't have to synchronize with others to access the data. Then it uses as many workers as there are cores to process those partitions. There's little point in starting 100 workers if there are only 4 cores. The other 96 workers will simply do nothing except add to the scheduling overheadwhich will automatically do the WaitAllthat's not what happens.Parallelwill use the current thread to process data, and since all cores are busy crunching data, it appears as if the thread is "blocked". It's notForEachAsyncreturns aTask, so it behaves as if you calledWhenAll, notWaitAllForEachandForEachAsyncpass state between workers and iterations though, something not possible with either a loop of tasks or `ActionBlock. And as the source shows it doesn't just start some tasks.