InvalidOperationException - object is currently in use elsewhere - red cross

前端 未结 4 1474
慢半拍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:17

    I had a similar problem with the same error message but try as I might, locking the bitmap didn't fix anything for me. Then I realized I was drawing a shape using a static brush. Sure enough, it was the brush that was causing the thread contention.

    var location = new Rectangle(100, 100, 500, 500);
    var brush = MyClass.RED_BRUSH;
    lock(brush)
        e.Graphics.FillRectangle(brush, location);
    

    This worked for my case and lesson learned: Check all the reference types being used at the point where thread contention is occurring.

提交回复
热议问题