title: how to lock global variable in multi thread?
body: <p>I  have two thread ,Each running two threads, no error appear. but run together,the  backgroundWorker2 tip :can't clone null.....(I check variable J value is greater than 100),in this case,how to lock global variable?</p>
  
  <pre><code>Bitmap img; //global variable
  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          int i = 0;
          do
          {
              img = (Bitmap)Image.FromFile(@"i:\1.jpg");
              img.Dispose();
              i++;
              backgroundWorker3.ReportProgress(i,"");
              Thread.Sleep(10);
          } while (!backgroundWorker4.CancellationPending);
      }
  
   private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
      {
          int j= 0;
          do
          {
              //img = (Bitmap)Image.FromFile(@"i:\1.jpg");
  
              if (img != null)
              {
                  lock (img)
                  {
                      Bitmap tempImg = (Bitmap)img.Clone();
                  }
              }
  
              j++;
              backgroundWorker4.ReportProgress(j, "");
              Thread.Sleep(10);
          } while (!backgroundWorker4.CancellationPending);
      }
  </code></pre>