title: How to do proper Parallel.ForEach, locking and progress reporting
body: <p>I'm trying to implement the <code>Parallel.ForEach</code> pattern and track progress, but I'm missing something regarding locking. The following example counts to 1000 when the <code>threadCount = 1</code>, but not when the <code>threadCount</code> > 1. What is the correct way to do this?</p>
  
  <pre><code>class Program
  {
     static void Main()
     {
        var progress = new Progress();
        var ids = Enumerable.Range(1, 10000);
        var threadCount = 2;
  
        Parallel.ForEach(ids, new ParallelOptions { MaxDegreeOfParallelism = threadCount }, id =&gt; { progress.CurrentCount++; });
  
        Console.WriteLine("Threads: {0}, Count: {1}", threadCount, progress.CurrentCount);
        Console.ReadKey();
     }
  }
  
  internal class Progress
  {
     private Object _lock = new Object();
     private int _currentCount;
     public int CurrentCount
     {
        get
        {
           lock (_lock)
           {
              return _currentCount;
           }
        }
        set
        {
           lock (_lock)
           {
              _currentCount = value;
           }
        }
     }
  }
  </code></pre>