views:

11

answers:

1

Hi there,

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

+1  A: 

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

Douglas
Thanks ill check it out
turtlepower
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
@Mark: Yes, that would be good too. I'd expect that to use the dispatcher internally.
Douglas
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
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