Bitmap region is already locked

G

Guest

I have an application that is capturing grayscale images from a frame grabber
(about 10 frames per second). The 1024 X 768 pixels are then copied to the
bitmapData section of a bitmap ( a background processing thread) and then
displayed by the UI thread.

The basic structure of the code is that the main form starts a new thread to
capture frames from the frame grabber. Using lockbits() and unlockbits() the
data is then copied to the one and only bitmap by the processing thread.
After the bitmapData section has been updated, that thread then raises an
event that the main UI thread has registered for to display the updated
bitmap. The call from the background thread back to the UI thread is
synchronous. This all works fine if no other UI components are being
change/painted. Occasionally, in the UI thread, when other UI events are
occurring (updating menu, etc) I get an “InvalidOperationException: Bitmap
region is already lockedâ€. If I just catch that infrequent exception and
throw it away everything works OK, but I am hoping to prevent that from
happening. The code that is actually displaying the bitmap, is an overridden
OnPaint() handler for the control that is being used to draw the bitmap.
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage( (Image) bitmap, 0, 0 );
}

I originally thought that there would be no contention for the bitmap since
the frame grabbing thread called the UI thread synchronously, but I guess the
UI thread must make some asynchronous calls of its own when handling all the
painting that is going on.

Any words of advice of how handle this situation?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top