I was under the impression that lock()
would prevent multiple threads from accessing an object simultaneously.
But, an InvalidOperationException (Object is currently in use elsewhere) is still frequently thrown by the following code:
lock (this)
{
localCopy = (Bitmap)this.bm.Clone();
int x, y;
float pX = this.p.x;
int width = localCopy.Width;
x = (int)Math.Round((double)(pX * (float)width));
if (x >= localCopy.Width) x = localCopy.Width - 1;
y = (int)Math.Round((double)(this.p.y * (float)localCopy.Height));
if (y >= localCopy.Height) y = localCopy.Height - 1;
colourPixel = localCopy.GetPixel(x, y);
}
Some things to note:
- I split up the calculation of
x
to isolate the cause of the exception. It appears to come from accessing the bitmap.
- I tried creating a local copy of the bitmap, but this just causes the same exception. I've tried
Clone()
ing and creating a new Bitmap. Neither works.
- I've tried locking on
this
(as seen) and on the bitmap object. Neither works.
Am I trying to use lock()
in a way I'm not supposed to? Have I misunderstood it's purpose? How can I prevent InvalidOperationException
s?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…