title: Parallel.ForEach returning inconsistent result
body: <p>I have a method which reads a text file which contains an int value per line, for making reading faster, i used <code>Parallel.ForEach</code>, but the behaviour what i am seeing is unexpected, i have 800 lines in the file but when i run this method, every time it returns different count of HashSet, what i have read after searching is <code>Parallel.ForEach</code> spawns multiple threads and it returns the result when all threads have completed their work, but my code execute contradicts, or i am missing something improtant here?</p>
  
  <p>Here is my method:</p>
  
  <pre><code>private HashSet&lt;int&gt; GetKeyItemsProcessed()
  {
     HashSet&lt;int&gt; keyItems = new HashSet&lt;int&gt;();
  
     if (!File.Exists(TrackingFilePath))
       return keyItems;
  
       // normal foreach works fine
  
       //foreach(var keyItem in File.ReadAllLines(TrackingFilePath))
       //{
       //    keyItems.Add(int.Parse(keyItem));
       //}
  
  
       // this does not return right number of hashset rows
       Parallel.ForEach(File.ReadAllLines(TrackingFilePath).AsParallel(), keyItem =&gt;
       {
           keyItems.Add(int.Parse(keyItem));
       });
  
  
      return keyItems;
  
  }
  </code></pre>