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
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.