title: Help with synchronization from a unit test in C#
body: <p>I'm testing a class that wraps BackgroundWorker to perform an operation away from the UI thread in my application.</p>
  
  <p>The test below fails if Timeout is exceeded and passes if progressEventCount reaches the expected number of events before then.</p>
  
  <p>My question is about synchronization.  asyncExecutor.Progressed is fired from the Thread Pool thread that BackgroundWorker is using and the test thread reads it back in the while loop.</p>
  
  <p>Am I using lock correctly?</p>
  
  <pre><code>    [Test]
      [Timeout(1250)]
      public void Execute()
      {
          var locker = new object();
          const int numberOfEvents = 10;
          const int frequencyOfEvents = 100;
          var start = DateTime.Now;
          int progressEventCount = 0;
  
          IGradualOperation tester = new TestGradualOperation(numberOfEvents, frequencyOfEvents);
  
          var asyncExecutor = new AsynchronousOperationExecutor();
  
          asyncExecutor.Progressed += (s, e) =&gt; { lock (locker) progressEventCount++; };
  
          asyncExecutor.Execute(tester);
  
          while (true)
          {
              int count;
              lock (locker)
              {
                  count = progressEventCount;
              }
              if (count &lt; numberOfEvents) continue;
              Assert.Pass("Succeeded after {0} milliseconds", (DateTime.Now - start).TotalMilliseconds);
          }
      }
  
  
  //  Implementation
  public class AsynchronousOperationExecutor
  {
      public void Execute(IGradualOperation gradualOperation)
      {
          var backgroundWorker = new BackgroundWorker {WorkerReportsProgress = true};
  
          backgroundWorker.DoWork += BackgroundWorkerDoWork;
          backgroundWorker.ProgressChanged += BackgroundWorkerProgressChanged;
          backgroundWorker.RunWorkerAsync(gradualOperation);
      }
  
      private void BackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          var myArgs = e.UserState as ProgressEventArgs;
          OnProgressed(myArgs);
      }
  
      static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
      {
          var workerThis = sender as BackgroundWorker;
          var operation = e.Argument as IGradualOperation;
  
          if (workerThis == null || operation == null) return;
  
          operation.Progressed += (s, e1) =&gt; workerThis.ReportProgress((int)e1.Percentage, e1);
  
          operation.Run();
      }
  
      private void OnProgressed(ProgressEventArgs e)
      {
          if (Progressed != null)
              Progressed(this, e);
      }
  
      public event EventHandler&lt;ProgressEventArgs&gt; Progressed;
  }
  
  
  //   Test Helper Class
  public class TestGradualOperation : IGradualOperation
  {
      private readonly int _numberOfEvents;
      private readonly int _frequencyMilliseconds;
  
      public TestGradualOperation(int numberOfEvents, int frequencyMilliseconds)
      {
          _numberOfEvents = numberOfEvents;
          _frequencyMilliseconds = frequencyMilliseconds;
      }
  
      public void Run()
      {
          for (int i = 0; i &lt; _numberOfEvents; i++)
          {
              Thread.Sleep(_frequencyMilliseconds);
              OnProgressed(new ProgressEventArgs(i, _numberOfEvents));
          }
      }
  
      private void OnProgressed(ProgressEventArgs e)
      {
          if (Progressed != null)
              Progressed(this, e);            
      }
  
      public event EventHandler&lt;ProgressEventArgs&gt; Progressed;
  }
  </code></pre>