Connecting Tech Pros Worldwide Forums | Help | Site Map

InvalidOperationException object is currently in use elsewhere - red cross

Newbie
 
Join Date: Jun 2009
Posts: 2
#1: Jun 29 '09
Hello

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.

The application works but on some machines I get the following error at random time

intervals(unpredictable)

Expand|Select|Wrap|Line Numbers
  1.  
  2. ************** Exception Text **************
  3. System.InvalidOperationException: The object is currently in use elsewhere.
  4.  
  5.  

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

From what I can tell this error comes from the control's onpaint event where I draw something else

on the picture

I tried using a lock there but no luck :(

The way I call the function that puts the image on the panel is as follows:

Expand|Select|Wrap|Line Numbers
  1. if (this.ReceivedFrame != null)
  2.                 {
  3.                     Delegate[] clients = this.ReceivedFrame.GetInvocationList();
  4.                     foreach (Delegate del in clients)
  5.                     {
  6.                         try
  7.                         {
  8.                             del.DynamicInvoke(new object[] { this, new StreamEventArgs(frame) });
  9.                         }
  10.                         catch { }
  11.                     }
  12.                 }
  13.  

this is the delegate:

Expand|Select|Wrap|Line Numbers
  1. public delegate void ReceivedFrameEventHandler(object sender, StreamEventArgs e);
  2.         public event ReceivedFrameEventHandler ReceivedFrame;
  3.  

and this is how the function inside the control code-behind registers to it:

Expand|Select|Wrap|Line Numbers
  1. Camera.ReceivedFrame += new Camera.ReceivedFrameEventHandler(camera_ReceivedFrame);
  2.  

I also tried
Expand|Select|Wrap|Line Numbers
  1. del.Method.Invoke(del.Target, new object[] { this, new StreamEventArgs(b) });
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. del.DynamicInvoke(new object[] { this, new StreamEventArgs(frame) });
  2.  

but no luck


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?

Can anyone help please? I am really stuck
Thank you in advance

Reply

Tags
c# .net, currently in use, invalidoperationexception, red cross