InvalidOperationException异常 - 对象是目前在其他地方使用 [英] InvalidOperationException - object is currently in use elsewhere

查看:2219
本文介绍了InvalidOperationException异常 - 对象是目前在其他地方使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过<一个了href="http://stackoverflow.com/questions/246058/system-invalidoperationexception-the-object-is-currently-in-use-elsewhere-ho">this SO质疑,但它并没有帮助。

I've gone through this SO question but it didn't help.

这里的情况就不同了。我使用Backgroundworkers。 1日的BackgroundWorker开始对用户的图像输入和内部firstbackgroundworker_runworkercompleted()我使用调用其他3 backgroundworkers经营

The case here is different. I'm using Backgroundworkers. 1st backgroundworker starts operating on the image input of user and inside firstbackgroundworker_runworkercompleted() I'm using calling 3 other backgroundworkers

 algo1backgroundworker.RunWorkerAsync();
 algo2backgroundworker.RunWorkerAsync();
 algo3backgroundworker.RunWorkerAsync();

这是在code每个:

algo1backgroundworker_DoWork()
{
 Image img = this.picturebox.Image;
 imgclone = img.clone();
 //operate on imgclone and output it
}

algo2backgroundworker_DoWork()
{
 Image img = this.picturebox.Image;
 imgclone = img.clone();
 //operate on imgclone and output it
}

类似的操作在其他的算法中做* backgrougrondworker_doWork()。

similar operations are done in other algo*backgrougrondworker_doWork().

现在有时我收到InvalidOperationException异常 - 对象是目前在其他地方使用。它很随意。我somtimes得到这个algo1backgroundworker_DoWork,有时在algo2backgroundworker_DoWork,有时在Application.Run(新myWindowsForm());

Now SOMETIMES I'm getting "InvalidOperationException - object is currently in use elsewhere". Its very arbitrary. I somtimes get this in algo1backgroundworker_DoWork and sometimes in algo2backgroundworker_DoWork and sometimes in Application.Run(new myWindowsForm());

我有没有线索发生了什么。

I've no clue about whats happening.

推荐答案

有内部GDI +是prevents从在同一时间访问一个位图两个线程锁。这不是一个阻塞类型的锁,它是一个程序员做错了什么事,我会抛出一个异常类型的锁。你的线程轰炸,因为你是克隆的图像(==访问一个位图)中的所有线程。您的UI线程轰炸因为它试图绘制位图(==访问一个位图),同时一个线程被克隆它

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.

您需要限制为只能有一个线程访问该位图。启动BGWs之前,克隆的UI线程的图像,每个BGW需要自己的形象的副本。更新在RunWorkerCompleted事件中PB的图像属性。你会失去一些并发这样但这是不可避免的。

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.

这篇关于InvalidOperationException异常 - 对象是目前在其他地方使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆