i am extremely new to C# so excuse me if i don’t explain this well.
i’m retrieving images from my computer camera, and along with displaying them in a PictureBox, i’m encoding them to jpegs and sending them to a shared dictionary. here’s my code:
void CurrentCamera_OnImageCaptured(object sender, CameraEventArgs e)
{
this.pictureBoxMe.Image = e.Image;
if (myName != "" && Form1.PicSent)
{
SendPic sendP = new SendPic((Image)e.Image.Clone());
new System.Threading.Thread(new System.Threading.ThreadStart(sendP.send)).Start();
}
}
public class SendPic
{
Image im;
public SendPic (Image im)
{
this.im = im;
}
public void send(){
Form1.PicSent = false;
var memoryStream = new MemoryStream();
im.Save(memoryStream, ImageFormat.Jpeg);
var byteArray = memoryStream.ToArray();
Form1.sd["/" + myName + "/video"] = byteArray;
memoryStream.Close();
Form1.PicSent = true;
}
}
the problem is that i’m getting the “Object is currently in use elsewhere.” error on the line: SendPic sendP = new SendPic((Image)e.Image.Clone());
based on other forum posts i’ve found, i already changed it so that the image is passed to the thread, and so that it’s a clone. however i’m still getting the same error (though it lasts longer before crashing now).
i read something about locking? how do i implement that in this case? or is there something else i need to do?
thanks.
It behaves as though the OnImageCaptured method runs on a thread. Which isn’t unlikely for camera interfaces. Set a breakpoint and use the debugger’s Debug + Windows + Threads window to see what thread is running this code.
The failure mode is then that the UI thread is accessing the image to paint the picture box, simultaneously with this worker thread calling Clone(). GDI+ does not permit two threads accessing the same image object at the same time. It would indeed be flaky, no telling at what exact moment in the time the UI thread starts painting. PicSent is another accident waiting to happen.