错误:对象当前正在其他位置使用。 [英] Error : Object is currently in use elsewhere.

查看:2022
本文介绍了错误:对象当前正在其他位置使用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序从DSLR相机连续读取图像流。

I have developed an application the continuous read image stream from a DSLR camera.

while (!liveViewExit)
        {
            // Create a Memory Stream 
            stream = new IntPtr();

            // Get the bitmap image from the DSLR
            bmp = GetEvfImage(stream);


            if (bmp != null)
            {

                // Crop the image.
                picImage = (System.Drawing.Image)bmp.Clone(new Rectangle(10, 0, 492, 768), bmp.PixelFormat);
                try
                {
                    if (picImage != null)
                        this.picLiveView.Image = (System.Drawing.Image)picImage.Clone();
                }
                catch (Exception ex)
                {
                    Utility.HandleError(ex);
                }


            }
        }



<运行一段时间后,我有这行代码的错误:

After running a while, I have this error for this line of code:

   this.picLiveView.Image = (System.Drawing.Image)picImage.Clone();


Object is currently in use elsewhere.(   at System.Drawing.Image.get_FrameDimensionsList()
   at System.Drawing.ImageAnimator.CanAnimate(Image image)
   at System.Drawing.ImageAnimator.ImageInfo..ctor(Image image)
   at System.Drawing.ImageAnimator.Animate(Image image, EventHandler onFrameChangedHandler)
   at System.Windows.Forms.PictureBox.Animate(Boolean animate)
   at System.Windows.Forms.PictureBox.Animate()
   at System.Windows.Forms.PictureBox.InstallNewImage(Image value, ImageInstallationType installationType)
   at System.Windows.Forms.PictureBox.set_Image(Image value)



我认为picLiveView PictureBox控件尚未准备好接受新图片。
任何想法如何检测PictureBox是否仍在使用中。

I think the picLiveView PictureBox control is not yet ready to accept new image. Any idea how on detect if PictureBox is still in used.

//已添加:

推荐答案

这是一个单线程,我认为picturebox不够快,不足以处理在while循环中的图片对象。是否有多个线程更新 picLiveView 映像?如果这样会解释这个问题。只需使用一个线程,并序列化更新 - 或者您可以使用锁来访问 picLiveView

Are multiple threads updating the picLiveView image? If so that would explain this problem. Just use one thread instead, and serialize the update - alternatively you could use a lock to access picLiveView:

private readonly object myLock = new object();
...


if (picImage != null)
{
   lock(myLock)
   {
      this.picLiveView.Image = (System.Drawing.Image)picImage.Clone();
   }
} 

这篇关于错误:对象当前正在其他位置使用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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