3

I've enocountered a problem while trying to use Emgu to capture images from a webcam. To do this task, Emgu uses unmanaged opencv libraries. So the problem is that I can't update my GUI (WPF Image control) from System.Timers.Timer Elapsed event. I know it's running in different thread, but hey, that's why I'm using a Dispatcher. It's the first time I can't do it with Dispatcher. Getting InvalidOperationException with "The calling thread cannot access this object because a different thread owns it.". I've spent the whole day searching for a solution, but still couldn't fix it. Any ideas why does it happen?

webcam.OnNewFrame += newBitmapSource => this.imgCaptured.Dispatcher.Invoke
            (
                new Action(delegate
                    {
                        this.imgCaptured.Source = newBitmapSource;
                    }),
                DispatcherPriority.Background
            );

The last thing on Stack Trace is: System.Windows.Threading.Dispatcher.VerifyAccess()

However, if I call Dispatcher.CheckAccess() it returns true.

UPDATE:

Finally I found it out by myself: the BitmapSource had to be created on UI Thread. It seems like it can't use this object otherwise.

Martynas
  • 1,064
  • 10
  • 21

1 Answers1

1

Did you try setting it to Non-Background thread. I wonder if that is an issue - updating UI from background thread.

Also, It says you must freeze Bitmap resources before trying to share them across different threades - worker thread and UI thread. Refer below links.

WPF Dispatcher {"The calling thread cannot access this object because a different thread owns it."}

http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/9223743a-e9ae-4301-b8a4-96dc2335b686

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25