The following example illustrates this problem by using a loop body that adds items to an instance of System.Collections.Generic.List. Running the example will either result in an unexpected number of items in the result collection or an exception when the state of the list is modified by two Tasks at once.

using System;

using System.Collections.Generic; using System.Threading.Tasks;

namespace Using_Standard_Collections { class Using_Standard_Collections { static void Main(string[] args) {

// create some source data int[] sourceData = new int[10000]; for (int i = 0; i < sourceData.Length; i++) { sourceData[i] = i;

// create a list to hold the results List<int> resultData = new List<int>();

Parallel.ForEach(sourceData, item => { resultData.Add(item);

Console.WriteLine("Results {0}", resultData.Count);

// wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine();

0 0

Post a comment