Here is the code
List something = new List();
Parallel.ForEach(anotherList, r =>
{
.. do some work
In order to prevent the issue, instead of List you may use ConcurrentQueue or similar Concurrent collections in your parallel part. Once the parallel task is done, you can put it in the List<T>
.
For more information take a look at System.Collections.Concurrent namespace to find the suitable collection for your use case.
I found that lock (yourObject)
also negates the threading problem