1

I have a Brush Colour which I would like to change every so and so on a thread.

static SolidColorBrush myBrush;

Thread changeColourThread = new Thread(changeColour);

static void changeColour()
{
       myBrush = new SolidColorBrush(Color.FromArgb(255, 33, 96, 22));
}

This returns an UnauthorizedAccessException, what's the best way to handle this?

Thanks

turtlepower
  • 369
  • 1
  • 5
  • 13

2 Answers2

1

You're going to need to use the dispatcher, try this thread.

Community
  • 1
  • 1
Douglas
  • 36,802
  • 9
  • 76
  • 89
  • 1
    You could of course also use a backgroundworker class too which can report back to the UI thread at certain points during its operations – Mark Oct 13 '10 at 09:49
  • @Mark: Yes, that would be good too. I'd expect that to use the dispatcher internally. – Douglas Oct 13 '10 at 09:55
  • hmmm, I wonder, I was under the impression that the Dispatcher just adds actions to the dispatcher queue that get processed when the UI thread is available. Where-as the BackgroundWorker class reports back to the calling thread (which may not be the UI thread) which does whatever it wants with the message. Not 100% sure though... – Mark Oct 13 '10 at 10:07
  • Having a peak in Reflector, it looks like BackgroundWorker calls ThreadPool.QueueUserWorkItem when it raises ProgressChanged events, where as Dispatcher raises OperationPosted events so it's presumably up to whatever attaches to that event to get the message onto the right thread. – Douglas Oct 13 '10 at 11:29
0

It looks like creating SolidColorBrush has to be done in UI thread (no idea why). I had similar problem and my solution is to return just Color and then use Converter to convert it into a Brush in .xaml.

Lukas Cenovsky
  • 5,476
  • 2
  • 31
  • 39