Getting Must create DependencySource on same Thread as the DependencyObject error

  • Thread starter Thread starter RichardR
  • Start date Start date
R

RichardR

Guest
I have an ObservableCollection of a class that contains couple properties, including one of type System.Windows.Media.Imaging.BitmapImage.

I'm trying to populate the collection using the Parallel library with this code:

thumbnails = new ObservableCollection<thumbnailClass>();
String[] files = System.IO.Directory.GetFiles(folderPath);

Parallel.ForEach(files (file) =>
{
thumbnails.Add(new thumbnailClass(file))
});


And the constructor for thumbnailClass is:

public thumbnailClass(String imagePath)
{
image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(imagePath);
image.DecodePixelWidth = 200;
image.EndInit();
}


When I try to use Parallel loops, I'm getting the "Must create DependencySource on the same Thread as the DependencyObject" error. When I just load the images using a basic foreach loop, no problem.

Does anyone have any suggestions on how to work around this issue?

Richard

Continue reading...
 
Back
Top