I\'ve gone through this SO question but it didn\'t help.
The case here is different. I\'m using Backgroundworkers. 1st backgroundworker starts operating on the image
In windows forms not only should you only access the controls from a single thread but that thread should be the main application thread, the thread that created the control.
This means that in DoWork you should not access any controls (without using Control.Invoke). So here you would call RunWorkerAsync passing in your image clone. Inside the DoWork event handler, you can extract the parameter from the DoWorkEventArgs.Argument.
Only the ProgressChanged and RunWorkerCompleted event handlers should interact with the GUI.
There's a lock inside GDI+ that prevents two threads from accessing a bitmap at the same time. This is not a blocking kind of lock, it is a "programmer did something wrong, I'll throw an exception" kind of lock. Your threads are bombing because you are cloning the image (== accessing a bitmap) in all threads. Your UI thread is bombing because it is trying to draw the bitmap (== accessing a bitmap) at the same time a thread is cloning it.
You'll need to restrict access to the bitmap to only one thread. Clone the images in the UI thread before you start the BGWs, each BGW needs its own copy of the image. Update the PB's Image property in the RunWorkerCompleted event. You'll lose some concurrency this way but that's unavoidable.
When I tried to access the main thread component (PictureBox) from different threads, I had also System.InvalidOperationException: Object is currently in use
MethodInvoker Delegate
solved the problem.
Invoke((MethodInvoker)(delegate ()
{
var image = PictureBox.Image;
}));
So it looks like your BackgroundWorkers are trying to access the same Windows Forms components at the same time. This would explain why the failure is random.
You'll need to make sure this doesn't happen by using a lock
, perhaps like so:
private object lockObject = new object();
algo1backgroundworker_DoWork()
{
Image imgclone;
lock (lockObject)
{
Image img = this.picturebox.Image;
imgclone = img.clone();
}
//operate on imgclone and output it
}
Note that I make sure that imgclone is local to this method - you definitely don't want to share it across all the methods!
On the other hand the same lockObject instance is used by all the methods. When a BackgroundWorker method enters its lock{}
section, others that come to that point will be blocked. So it's important to make sure that the code in the locked section is fast.
When you come to "output" your processed image, be careful too to make sure that you don't do a cross-thread update to the UI. Check this post for a neat way to avoid that.