I have a BitmapFrame object created by a working thread (not UI thread) and placed in a static collection.
Then, I have a different working thread assigning this object to an Image object owned by the UI thread.
As you can imagine, I can’t access theImage object (As it belongs to the UI thread) and I get: "calling thread cannot access the object because different thread owns it".
So, I tried solving it by doing the following:
imageMainImage.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action<ImageItem>(delegate(ImageItem A) {
imageMainImage.Source = A.ManipulatedPreview;
}), II);
II is the Image item (created by a working thread and available through a static class) and imageMainImage is the Image object owned by the UI thread.
But now, again, I get the "calling thread..." but this time I get it because the II object belongs to a different thread (the first working thread).
What I’m trying to do is have one thread work with 2 Image elements that belong to different threads.
I’m trying to work the whole process differently but was wondering, is there a solution to this?
thanks.
You need to do a CheckAccess() call on the Dispatcher first to see if you are on the UI thread and therefore capable of updating the UI item.
Here is a helper function i wrote to update my comboboxes in WPF, you can easily adapt it to suit an Image control, and it is a good example of the CheckAccess call:
So the function checks if it has access, if it does it assigns the datasource. If it doesn’t, then it calls itself back but this time within the context of the UI thread.
There is probably a more succinct way to code the delegate creation, but that isn’t so good for people trying to understand the sample, hence the explicit delegate definition.