Got "Index out of bounds" Error on List.Add() in c# Parallel.ForEach

Viewed 6772

Here is the code

List<string> something = new List<string>();
Parallel.ForEach(anotherList, r =>
     {
            .. do some work
             something.Add(somedata);
      });

I get the Index out of bounds error around 1 time per hundred run. Is there anyway to prevent the conflict (I assume) caused by threading?

2 Answers
Related