title: Object is currently in use elsewhere
body: <p>I'm getting this error, and it looks like it's because the same Bitmap object is being accessed by different threads. However I am using locks everywhere with it.</p>
  <pre><code>public class MySingleInstanceClass
  {
      private Object locker = new Object();
  
      private Bitmap myImage = new Bitmap(100, 100);
  
      public Bitmap MyImage
      {
          get
          {
              lock (locker)
                  return myImage;
          }
          private set
          {
              lock (locker)
                  myImage = value;
          }
      }
  
      private void Refresh()
      {
          lock (locker)
          {
              var g = Graphics.FromImage(myImage);
              // do more processing
          }
      }
  }
  </code></pre>
  <p>Class <code>MySingleInstanceClass</code> will have only one instance. Calls to <code>MyImage</code> and <code>Refresh()</code> may come from different threads. As far as I understand, the code inside <code>lock(locker)</code> will not be executed until it's finished in another thread, but I still get the error. Can anyone point a flaw in the code?</p>
  <p>Exception looks like that:</p>
  <blockquote>
  <p>A first chance exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll</p>
  <p>Error: Object is currently in use elsewhere.</p>
  <p>at System.Drawing.Graphics.FromImage(Image image)</p>
  <p>at (points to the line containing var g = Graphics.FromImage(myImage);)</p>
  </blockquote>