Parallel.ForEach() vs. foreach(IEnumerable.AsParallel())

They do something quite different.

The first one takes the anonymous delegate, and runs multiple threads on this code in parallel for all the different items.

The second one not very useful in this scenario. In a nutshell it is intended to do a query on multiple threads, and combine the result, and give it again to the calling thread. So the code on the foreach statement stays always on the UI thread.

It only makes sense if you do something expensive in the linq query to the right of the AsParallel() call, like:

 var fibonacciNumbers = numbers.AsParallel().Select(n => ComputeFibonacci(n));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)