title: Parallel.ForEach not storing data in variable accurately
body: <p>First off, I apologize for the title, I am unsure of how to word this question.</p>
  
  <p>I currently have a <code>Parallel.ForEach</code> looping through a directory filled with .txt files.</p>
  
  <p>What I am doing inside of this is grabbing specific data that varies from file-to-file.</p>
  
  <p>Whenever all of the needed data is grabbed, it is then outputted to a file.</p>
  
  <p>Everything is accurate except for the ID numbers inside each text file. The ID numbers are not lining up with their corresponding file. It's outputted in an arbitrary manner. The names and date's line up fine, but not the actual ID numbers inside of the text files. I am unsure of how to proceed with this as the code looks fine to me. The only thing I can think of is <code>AppendAllText</code> is consistently opening and closing the final text file, which in turn disallows accurate data to be outputted. Below is my code.</p>
  
  <pre><code>        Parallel.ForEach(directoryInfo.GetFiles("*.txt"), (file, state) =&gt;
          {
              using StreamReader sr = File.OpenText(file.FullName);
              string user = sr.ReadToEnd();
              if (user.Contains("ID:"))
              {
                  ID = IDRegex.Match(user).Value.Replace("ID:", string.Empty);
  
              }
              else if (user.Contains("ID="))
              {
                  ID = IDDRegex.Match(user).Value.Replace("ID=", string.Empty);
              }
  
              this.Dispatcher.Invoke(() =&gt;
              {
                  //count++;
                  //Current.Content = file.Name;
                  if (user.Contains(users.Text))
                  {
                      File.AppendAllText(Idir + "IReport-" + IUser.Text + ".txt",
                                         String.Format("{0,-16} {1,27}", file.Name.Replace(".txt", ""), (file.LastWriteTime.Date).ToString()).Replace("12:00:00 AM", "") +
                                         String.Format("{0, 18}", ID) + Environment.NewLine + Environment.NewLine);
                  }
              });
          });
  </code></pre>