2

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 InvalidOperationExceptions?

Tom Wright
  • 11,278
  • 15
  • 74
  • 148

2 Answers2

4

Probably try using an object for locking purpose rather than locking "this".

A class level variable

private static object syncRoot = new Object();

and when you are using

lock (syncRoot)
{
....
}
Milind Thakkar
  • 980
  • 2
  • 13
  • 20
  • Thanks. I tried this, but accessing `this.bm` still results in an InvalidOperationException. – Tom Wright Oct 03 '12 at 13:54
  • Tom, is it possible that issue is other than lock ? I ask because the description of InvalidOperationException says "InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments." And BitMap.Clone really takes couple of different input parameters ! – Milind Thakkar Oct 03 '12 at 13:59
  • You may have misread that. Since it says "reasons OTHER than invalid arguments", the parameters of `Bitmap.Clone()` seem irrelevant. Besides, the issue occurs even when I use new `Bitmap(Bitmap)`. – Tom Wright Oct 03 '12 at 14:08
  • Yup. Misread that..probably http://stackoverflow.com/questions/1851292/invalidoperationexception-object-is-currently-in-use-elsewhere may throw some light. – Milind Thakkar Oct 03 '12 at 14:14
  • @TomWright: As stated in the link above, the thread which creates the bitmap locks it. Some locking in the GDI. – Milind Thakkar Oct 03 '12 at 17:19
1

I eventually got to the bottom of this. I moved the locks into the getter / setter methods of the bitmap property and implemented a "deep copy" method to free up the property as soon as possible.

private static object syncRoot = new Object();
private Bitmap _bm;
private Bitmap bm
{
    get
    {
        lock (syncRoot)
            return this._bm.DeepClone();
    }
    set
    {
        lock (syncRoot)
        {
            this._bm = value.DeepClone();
        }
    }
}

The DeepClone() extension method was cribbed from an answer to another question:

public static T DeepClone<T>(this T a)
{
    using (MemoryStream stream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, a);
        stream.Position = 0;
        return (T)formatter.Deserialize(stream);
    }
}
Community
  • 1
  • 1
Tom Wright
  • 11,278
  • 15
  • 74
  • 148