Error : Object is currently in use elsewhere.

耗尽温柔 提交于 2019-12-12 19:59:53

问题


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)

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.

// Added:

It is a single thread. I think the picturebox is not fast enough to process the picture object in the while loop.


回答1:


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();
   }
} 



回答2:


I know am late...but try this, if someone have same issue..

if (bmp != null)
            {

                // Crop the image.
                picImage = (System.Drawing.Image)bmp.Clone(new Rectangle(10, 0, 492, 768), bmp.PixelFormat);

    **Bitmap img = new Bitmap(picImage);
    picImage.Dispose();
    picImage = null;**

      try
                {
                    if (picImage != null)
                        **this.picLiveView.Image = img;**
                }
                catch (Exception ex)
                {
                    Utility.HandleError(ex);
                }


            }


来源:https://stackoverflow.com/questions/4824027/error-object-is-currently-in-use-elsewhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!