Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:41:23+00:00 2026-06-12T14:41:23+00:00

I was under the impression that lock() would prevent multiple threads from accessing an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:41:24+00:00Added an answer on June 12, 2026 at 2:41 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was under impression that rm -r *.xml would remove all file from parent
I was under the impression that when you called Flush() in a StreamWriter object
I was under the impression that this was formed correctly, but here it is
I was under the impression that Compass could generate sprites from image files in
I was under the impression that all value types inherit from System.ValueType , and
I was under the impression that not calling Response.Flush would ensure that Connection: Close
I was under the impression that QueryPerformanceCounter was actually accessing the counter that feeds
I was under the impression that any function in C would support only one
So I was under the impression that rendering designs on the HTML5 canvas would
I was under the impression that it was possible to access data from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.