0

I am building a WPF program that does some image acquisition, processing and displaying with a Image object. Currently I summon new a thread from c++ to do the acquisition and processing, then update the Image through callbacks.

It seems when I access the Image.source in the callback, it throws a exception of something like "cannot access this object because another thread own it". I tried to construct the Image and access the Image.source both in the STA thread but the same exception occurs. How should I deal with this?

Much appreciated.

Jason M
  • 411
  • 5
  • 19

2 Answers2

3

If you create an ImageSource on a background thread, you can call its Freeze method to make it cross-thread accessible.

You would then set the Source property of an Image control in the UI thread, by calling Dispacher.Invoke:

var bitmap = new BitmapImage();
bitmap.BeginInit();
...
bitmap.EndInit();
bitmap.Freeze();

image.Source.Dispatcher.Invoke(() => image.Source = bitmap);
Clemens
  • 123,504
  • 12
  • 155
  • 268
-3
 if (InvokeRequired)
 {
    this.Invoke(new Action<source>(ImageObject), new object[] {value});
    return;
 }

OR

Invoke((MethodInvoker)delegate {
MainForm.UpdateImageSource(source); 
});

OR

Call Image.source = object

Afnan Makhdoom
  • 654
  • 1
  • 8
  • 20