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

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

问题描述

我有一个C#桌面应用程序中,我创建continously一个线程从源(它实际上是一台数码相机)获得的图像,并把它在GUI面板(panel.Image = IMG)上(必须是另一个线程,因为它是一个控制的code-落后。

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.

应用程序的工作,但在某些机器上,我得到以下错误随机时间间隔(UN predictable)

The application works but on some machines I get the following error at random time intervals(unpredictable)

************** Exception Text **************
System.InvalidOperationException: The object is currently in use elsewhere. 

然后在面板变成了红叉,红色的X - 我认为这是无效的图片图标是从属性编辑。该应用程序保持工作,但面板永远不会更新。

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.

这是我可以告诉这个错误来自控件的OnPaint事件,我画上的画面别的东西。

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:

if (this.ReceivedFrame != null)
{
    Delegate[] clients = this.ReceivedFrame.GetInvocationList();
    foreach (Delegate del in clients)
    {
        try
        {
            del.DynamicInvoke(new object[] { this, 
                new StreamEventArgs(frame)} );
        }
        catch { }
    }
}

这是委托:

public delegate void ReceivedFrameEventHandler(object sender, StreamEventArgs e);
    public event ReceivedFrameEventHandler ReceivedFrame;

这是怎么里面的功能控制code-背后注册到它:

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

Camera.ReceivedFrame += 
    new Camera.ReceivedFrameEventHandler(camera_ReceivedFrame);

我也试过

del.Method.Invoke(del.Target, new object[] { this, new StreamEventArgs(b) });

而不是

del.DynamicInvoke(new object[] { this, new StreamEventArgs(frame) });

但没有运气

有谁知道我怎么能解决这个错误,或者至少以某种方式捕获错误,使线程把图像面板上再次?

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?

推荐答案

这是因为GDI +图片类不是线程安全的。 Hovewer您可以通过每次使用锁避免出现InvalidOperationException当你需要访问的图像,例如绘画或获取图像大小:

This is because Gdi+ Image class is not thread safe. Hovewer you can avoid InvalidOperationException by using lock every time when you need to Image access, for example for painting or getting image size:

Image DummyImage;

// Paint
lock (DummyImage)
    e.Graphics.DrawImage(DummyImage, 10, 10);

// Access Image properties
Size ImageSize;
lock (DummyImage)
    ImageSize = DummyImage.Size;

BTW,不需要调用,如果你将使用上面的图案。

BTW, invocation is not needed, if you will use the above pattern.

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

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