0

I have a list of objects which among other properties contain a Brush (property name "Color").

In a Backgroundworker I want to write the contents of these objects to a file. Everything works fine until I try to read the color of the brush property.

//this works fine
var brush = myObject.Color;

//this fails with InvalidOperationException
var c= ((SolidColorBrush)myObject.Color).Color;

It seems as if my code is somehow modifying the color but I don't see where/how. Is there a way to read-only access the color of the brush?

Regards,

tabina

tabina
  • 1,095
  • 1
  • 14
  • 37

2 Answers2

0

Do i sense that you're accessing UI objects on a thread that didn't create them. You cannot access UI objects from a BGW thread. That's the golden rule not to forget using BGW class

this-Me
  • 2,139
  • 6
  • 43
  • 70
0

If there is no need to modify the objects after they have been added to that list, you might try to Freeze them before they get accessed from a different thread.

Make sure that the Freeze call happens in the UI thread, where you created those objects. You might also check if freezing is possible at all by calling CanFreeze beforehand.

Clemens
  • 123,504
  • 12
  • 155
  • 268