Hi all!
I have WPF application that performs some calculations in BackgroundWorker. The problem is that when I try to update property (which calls NotifyPropertyChanged in setter) in RunWorkerCompleted event handler I get InvalidOperationException - The calling thread cannot access this object because a different thread owns it.
This MSDN article says that BackgroundWorker handles thread synchronization itself so I shouldn't care about using Dispatcher. But I see that it doesn't handle NotifyPropertyChanged correctly.
Can anybody help me with this issue?
EDIT
Here is my code (sorry for some irrelevant features):
backgroundWorker.DoWork += delegate(object sender, DoWorkEventArgs args)
{
var action = (Func<Bitmap>) args.Argument;
args.Result = BitmapUtil.BitmapSourceFromBitmap(action());
};
backgroundWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs args)
{
if (args.Cancelled || (args.Error != null))
{
return;
}
ImageProcessed = (BitmapSource) args.Result;
};
...
public BitmapSource ImageProcessed
{
get { return imageProcessed; }
set
{
imageProcessed = value;
OnPropertyChanged(VMUtil.GetNameOf<ImageAnalyzerViewModel>(vm => vm.ImageProcessed));
}
}