title: process files in parallel C#
body: <p>I have this code which reads all words from the files and assigns ID to each unique word and adds it to a Dictionary. I need to make it run in parallel to increase efficiency of the application. I have tried using <code>Parallel.ForEach</code> instead of <code>foreach</code> however, using lock to add new word and ID to the Dictionary does not increase efficiency at all... Could you guys help me with this by telling what are the best ways I could parallelize this code? </p>
  
  <pre><code>    //static object locker = new object();
      string[] fnames; // Files are collected from a save file dialog
      Dictionary&lt;string, IndexEntry&gt; ID = new Dictionary&lt;string, IndexEntry&gt;(); 
      foreach (var fname in fnames)
          {
  
              string[] lines = File.ReadAllLines(fname);
              for (int i = 0; i &lt; lines.Length; i++)
              {
                  string[] Raw = Regex.Split(lines[i], @"\W+");
  
                  for (int j = 0; j &lt; Raw.Length; j++)
                  {
                      string z = Raw[j];
  
                      if (!ID.ContainsKey(z))
                      {
                          ID.Add(z, new IndexEntry());
                      }
                  }
  </code></pre>