In playing around with the BackgroundWorker, searching for examples on how to cancel it, I have come across this:
c# Code:
  1. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  2.         {
  3.            
  4.             for (int i = 0 ; i < 100 ; i++)
  5.             {
  6.                 lock (backgroundWorker1)
  7.                 {
  8.                     if (backgroundWorker1.CancellationPending)
  9.                     {
  10.                         e.Cancel = true;
  11.                         break;
  12.                     }
  13.                 }
  14.  
  15.                 System.Threading.Thread.Sleep(100);
  16.  
  17.                 backgroundWorker1.ReportProgress(0, i);
  18.             }
  19.         }
The code is only doing some pointless loop, but my question is - Is the 'lock' operation necessary? Could reading the CancellationPending property at the same time CancelAsync() is called without locking the worker object cause a problem perhaps?

I haven't seen this used anywhere else in the other examples I have found, so just looking for some opinions.

Thanks.