You are faced with what is called Race Condition . Because the List collection in .Net is not thread safe, its operations, such as Add() , are not atomic. Basically, calling Add () in one thread can destroy another Add () thread before it finishes. You need thread safe data collection for your code.
Try the following:
using System.Threading.Tasks; class Program { public const int count = 3000; static ConcurrentBag<int> bag = new ConcurrentBag<int>(); static void DoWork(int i) { bag.Add(i); } static void Main(string[] args) { while (true) { Stopwatch s = new Stopwatch(); s.Start(); Parallel.For(0, count + 1, DoWork); s.Stop(); Console.WriteLine("\n Elapsed: " + s.Elapsed.ToString()); Console.WriteLine("Expected: {0}", count + 1); Console.WriteLine("count: {0}", bag.Count); Console.ReadKey(); bag = new ConcurrentBag<int>(); } } }
ConcurrentBag is the closest to a thread safe list. Just remember, since we are dealing with unknown planning, integers will not be in order.
source share