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 9227141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:59:46+00:00 2026-06-18T04:59:46+00:00

I need to obtain a lock in two different threads in order to access

  • 0

I need to obtain a lock in two different threads in order to access a Bitmap (which is populated from a webcam) in EmguCv.
I have a “GetFrame” functions that queries the camera and places what it returns into a .NET Bitmap. I have two threads which need to access this Bitmap, one needs to write to the Bitmap and assign the Bitmap to a picture box, and the other needs to read the Bitmap, convert it to an Image object and assign that to an EMGU ImageBox.
I first lock on an arbitrary Object, then I do my operations. The code is as follows (_Camera.LiveFrame is the Bitmap):

Writing/Reading Thread:

while (_CaptureThreadRunning)
{
   lock (_Camera)
   { 
      // _Camera.GetFrame writes to the Bitmap
      if (_VideoPlaying && _Camera.GetFrame(500)) 
           pbLiveFeed.Invalidate();
    }
}
_Camera.CloseCamera(true);
_CaptureExitEvent.Set();           // Set to signal captureThread has finished

Reading/ImageBox Thread:

while (_ProcessThreadRunning)
{
   lock (_Camera)
   {
      //  _Camera.LiveFrame is the Bitmap
      procImage = new Image<Bgr, int>((Bitmap)_Camera.LiveFrame.Clone());          
      procImage.Draw(new Rectangle(10,20,20,15),new Bgr(Color.LightGreen), 5);

      ibProcessed.Image = procImage;
      ibProcessed.Invalidate();
    }
}
_ProcessExitEvent.Set();

This runs fine most of the time but every now and then I get a “Object is in use elsewhere” error when I try to Clone() the Bitmap. Is this not the proper way to lock? I don’t see why this would cause a problem.

ps. My threads can no longer exit gracefully either. My .Set() calls outside of my loops are never called. I am guessing the threads are deadlocked?

  • 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-18T04:59:48+00:00Added an answer on June 18, 2026 at 4:59 am

    GDI+ has a locking mechanism which prevents two threads from using a Bitmap object–this is the error you’re receiving.

    You are trying to access the bitmap while the UI thread is already accessing it. For example, 1) you assign the bitmap to the picture box, 2) the picture box invalidates and then repaints, 3) you exit the write/read thread lock, and then 4) the reading/imagebox thread is trying to access the same Bitmap while the repainting is still occurring.

    To solve the problem, just make a copy of the bitmap, and use that copy to manipulate. Whatever you give the picture box, don’t assume you can touch that again from a non-UI thread.

    For example, in _Camera.GetFrame:

    // Get the bitmap from the camera
    capturedBitmap = GetFromCamera();
    
    // Clone the bitmap first before assigning to the picture box
    _Camera.LiveFrame = new Bitmap(capturedBitmap);
    
    // Assign to the picture box
    pbLiveFeed.Image = capturedBitmap;
    

    Now, _Camera.LiveFrame should be accessible from the thread, as long as you have proper locking.

    A couple of other issues I would think about here:

    1. You mention you are locking on an “arbitrary object”, but _Camera seems to be anything but that–it is an object which could be used elsewhere in unpredictable ways. I would suggest making an object which is only used for locking, e.g.

      object lockObject = new lockObject;
      lock (lockObject)
      {
          // put your synchronized code here
      }
      
    2. Bitmap.Clone() creates a bitmap which shares pixel data with the original bitmap. When you convert to the image object to assign to the EMGU ImageBox, you are using that clone, which maintains a reference to the bitmap. So, it seems safer to me to just create a new bitmap, rather than to use Clone() in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table from which I need to obtain rows ordered by a
I have a very huge file in which I need to obtain every nth
I need to obtain delivery tracking details from the Canada Post website, which does
I need to obtain the value from the Label Template Field which is bind
I need to obtain the code from a web, which is in part pure
I have written a small java application for which I need to obtain performance
I need to obtain path from a FileField , in order to check it
I need to obtain a Date object from this String:This is a example) M-27\nJUN-2012
I need to obtain actual height of several different elements (for the sake of
I need to obtain the Thread Id from a window create in runtime with

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.