title: InvalidOperationException - object is currently in use elsewhere - red cross
body: <p>I have a C# desktop application in which one thread that I create continously gets an image from a source(it's a digital camera actually) and puts it on a panel(panel.Image = img) in the GUI(which must be another thread as it is the code-behind of a control.</p>
  
  <p>The application works but on some machines I get the following error at random time intervals(unpredictable)</p>
  
  <pre><code>************** Exception Text **************
  System.InvalidOperationException: The object is currently in use elsewhere. 
  </code></pre>
  
  <p>Then the panel turns into a red cross, red X - i think this is the invalid picture icon that is editable from the properties. The application keeps working but the panel is never updated.</p>
  
  <p>From what I can tell this error comes from the control's onpaint event where I draw something else on the picture.</p>
  
  <p>I tried using a lock there but no luck :(</p>
  
  <p>The way I call the function that puts the image on the panel is as follows:</p>
  
  <pre><code>if (this.ReceivedFrame != null)
  {
      Delegate[] clients = this.ReceivedFrame.GetInvocationList();
      foreach (Delegate del in clients)
      {
          try
          {
              del.DynamicInvoke(new object[] { this, 
                  new StreamEventArgs(frame)} );
          }
          catch { }
      }
  }
  </code></pre>
  
  <p>this is the delegate:</p>
  
  <pre><code>public delegate void ReceivedFrameEventHandler(object sender, StreamEventArgs e);
      public event ReceivedFrameEventHandler ReceivedFrame;
  </code></pre>
  
  <p>and this is how the function inside the control code-behind registers to it:</p>
  
  <pre><code>Camera.ReceivedFrame += 
      new Camera.ReceivedFrameEventHandler(camera_ReceivedFrame);
  </code></pre>
  
  <p>I also tried </p>
  
  <pre><code>del.Method.Invoke(del.Target, new object[] { this, new StreamEventArgs(b) });
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>del.DynamicInvoke(new object[] { this, new StreamEventArgs(frame) });
  </code></pre>
  
  <p>but no luck</p>
  
  <p>Does anyone know how I could fix this error or at least catch the error somehow and make the thread put the images on the panel once again?</p>