List entry is really unsafe for multi-threaded entries. You need to either use lock to synchronize access, or use a collection of type ConcurrentQueue , which is designed for multi-threaded access.
Lock example (assuming list is local to the method)
List<SomeType> list = new List<SomeType>(); settings.AsParallel().ForAll(setting => { lock (list) { list.AddRange(GetSomeArrayofSomeType(setting)); } });
Or is it better to use SelectMany instead of ForEach
var list = settings .AsParallel() .SelectMany(setting => GetSomeArrayOfSomeType(setting)) .ToList();
source share