InvalidOperationException - object is currently in use elsewhere

前端 未结 4 1211
再見小時候
再見小時候 2020-11-28 13:25

I\'ve gone through this SO question but it didn\'t help.

The case here is different. I\'m using Backgroundworkers. 1st backgroundworker starts operating on the image

4条回答
  •  無奈伤痛
    2020-11-28 13:58

    There's a lock inside GDI+ that prevents two threads from accessing a bitmap at the same time. This is not a blocking kind of lock, it is a "programmer did something wrong, I'll throw an exception" kind of lock. Your threads are bombing because you are cloning the image (== accessing a bitmap) in all threads. Your UI thread is bombing because it is trying to draw the bitmap (== accessing a bitmap) at the same time a thread is cloning it.

    You'll need to restrict access to the bitmap to only one thread. Clone the images in the UI thread before you start the BGWs, each BGW needs its own copy of the image. Update the PB's Image property in the RunWorkerCompleted event. You'll lose some concurrency this way but that's unavoidable.

提交回复
热议问题