I'm working on a C# WPF application which loads a lot of images and displays it as thumbnails. I'd like to do it in a multi-threaded way. Therefore I tried to implement a BackgroundWorker.
The code of the BackgroundWorker's DoWork():
string[] files = e.Argument as string[];
foreach (string file in files)
{
ImageModel image = new ImageModel();
image.FilePath = file;
_importWorker.ReportProgress(1, image);
_imageCollectionVM.Images.Add(image); // also tried this one in ReportProgress()
}
In my XAML code I bind to the BitmapImage property of ImageModel. (AsyncState=True doesn't help.) Here I get this error: "DependencySource" and "DependencyObject" have to be in the same thread.
<Image Source="{Binding BitmapImage}" />
If I comment this out, the image seems to be imported but I cannot access it, e.g. by selecting it in a ListView. In its SelectionChanged it says then that this object is possessed by another thread.
How do I solve these problems? Thanks in advance!