title: Calling Parallel.ForEach from a background worker thread
body: <p>I had an issue where I were calling the <code>Parallel.ForEach</code> from the UI thread which resulted in the UI freezing, and the first thread running very slow and not keeping up with the rest. </p>
  
  <p>I read on here: <a href="https://stackoverflow.com/questions/8365346/why-does-this-parallel-foreach-code-freeze-the-program-up">Why does this Parallel.ForEach code freeze the program up?</a> that this is a bad thing to do, and then I found the code <code>Task.Factory.StartNew(() =&gt;</code> to wrap around the <code>Parallel.ForEach</code> command in order to run it from a background worker instead.</p>
  
  <p>The issue is that I keep getting an exception for the <code>Parallel.ForEach</code> code, but with no information other than <code>One or more errors occurred</code> from <code>e.Message</code>.</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt;
  {
      try
      {
          Parallel.ForEach&lt;ListViewItem&gt;(filesListView.Items.Cast&lt;ListViewItem&gt;(), new ParallelOptions() { MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 1.0)) }, (item, state) =&gt;
          {
              Thread.Sleep(100);
              if (CallToStop == true)
              {
                  state.Break();
              }
  
              internalProcessStart(item);
          });
      }
      catch (Exception e)
      {
          MessageBox.Show(e.Message);
      }
  });
  </code></pre>