InvalidOperationException - object is currently in use elsewhere - red cross

前端 未结 4 1475
慢半拍i
慢半拍i 2020-12-03 16:38

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

4条回答
  •  执念已碎
    2020-12-03 17:28

    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, invocation is not needed, if you will use the above pattern.

提交回复
热议问题