title: Monitoring Progress in Parallel.ForEach
body: <p>Attempting to monitor progress of a Parallel.ForEach loop I have tried the suggestion put forward in <a href="https://stackoverflow.com/questions/3807041/checking-status-of-parallel-foreach-loop">this question</a> but unfortunately I have still been unable to accomplish what I wanted.</p>
  
  <p>Basically the first problem I ran into when I attempted the implementation suggested (using a timer) was that the Parallel.ForEach method is a blocking call and hence the timer-tick callback was not occurring.</p>
  
  <p>So I tried putting the Parallel.ForEach loop inside of a background worker thread. Which did infact allow for the timer-tick event to occur but my counter value is never updated until the ForEach operation is complete.</p>
  
  <p>Here is the basic idea of the code (with the backgroundworker).</p>
  
  <pre><code>private StockList _StockListToProcess = null;
  
  private static Int64 ItemsProcessed = 0;
  
  private System.Windows.Threading.DispatcherTimer _timer = null;
  
  private System.ComponentModel.BackgroundWorker _backWorker = null;
  
  progressBar1.Minimum = 1;
  progressBar1.Maximum = this._StockListToProcess.Count;
  
  MainWindow.ItemsProcessed = 0;
  
  this._timer = new System.Windows.Threading.DispatcherTimer();
  this._timer.Interval = TimeSpan.FromMilliseconds(100);
  this._timer.Tick += timer_Tick;
  this._timer.Start();
  
  this._backWorker = new System.ComponentModel.BackgroundWorker();
  
  this._backWorker.DoWork += delegate(object o, System.ComponentModel.DoWorkEventArgs args)
  {
      Parallel.ForEach(this._StockListToProcess, new ParallelOptions() { MaxDegreeOfParallelism = 5 },
                       (Stock stock) =&gt;
                           {
                               MyWebServiceClient serviceClient = new MyWebServiceClient ();
                               MyWebServiceClient.ResponseEnum result = (MyWebServiceClient .ResponseEnum)serviceClient.SetProductPricing(token.LoginName, token.LoginPassword, token.SiteID.ToString(), stock.ProductCode, stock.ProductPrice);
                               System.Threading.Interlocked.Increment(ref MainWindow.ItemsProcessed);
                           });
  
      this._timer.Stop();
  };
  
  private void timer_Tick(object sender, EventArgs e)
  {
      progressBar1.Value = MainWindow.ItemsProcessed;
  }
  </code></pre>
  
  <p><strong>What am I missing?</strong></p>