1

I my kinect application I have main thread which is resposible for comunication between UI thread and other threads. I am no able to make a copy of WriteableBitmap generated from kinect and pass this WriteableBitmap image to separated thread with EmguCV processing. I was trying everything: Clone, CloneCurrentValue, BlockingCollection, but there are alwas some problems like:

The Calling thread cannot access this object because a different thread owns

Or processing data is wrong. This is main loop in my app;

WritableBitmap color; WritableBitmap depth; 
while (true) {          
    kinect.updateFrames();                
    ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
    {
       color = kinect.video.getBitmapColor();                   
       depth = kinect.video.getBitmapDepth();
    }));
    updateDetectors(color,depth); // Other thread
 }
Dariusz Filipiak
  • 2,858
  • 5
  • 28
  • 39

1 Answers1

2

Without a good, minimal, complete code example that reliably reproduces the problem, it's difficult if not impossible to know what the exact problem is, never mind how to fix it. That said…

As you're probably aware, WriteableBitmap inherits DispatcherObject, and must be accessed only within the dispatcher thread that owns it.

Presumably, the call to kinect.updateFrames() is what actually creates the objects, and so one obvious solution would be to call that method in the invoked anonymous method instead of just before it.

If for some reason that's not feasible, an alternative would be to freeze the bitmaps before trying to use them in the wrong thread. E.g.:

kinect.updateFrames();                
color = kinect.video.getBitmapColor();
depth = kinect.video.getBitmapDepth();

color.Freeze();
depth.Freeze();

ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
{
    // use color and depth in other thread
}));

Barring any of that, you can access the bitmaps' data directly (e.g. CopyPixels() or Lock()/BackBuffer) and use that data to create new bitmaps on the correct thread.

If none of the above proves useful to you, please provide a good code example as described in the link provide above.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136