blah blah blah is here! blah blah » Close

up0down
link

i have a program that needs to change the color of a rectangle. but i'm using threads. here's what i have

mySolidColorBrush.Dispatcher.Invoke(DispatcherPriority.Send, new FillBoxColor(fillBoxColor), Colors.Green;

delegate void FillBoxColor(Colors x);
void fillBoxColor(Colors x)
{
mySolidColorBrush.Color = x;
rctIsConnected.Fill = mySolidColorBrush;
}


i always get the cannot convert system.windows.media.color to system.windows.media.colors error. any ideas?

last answered one year ago

1 answers

up0down
link

I think that what you want is :

mySolidColorBrush.Dispatcher.Invoke(DispatcherPriority.Send, new FillBoxColor(fillBoxColor), Colors.Green);

delegate void FillBoxColor(object x); // not Colors
void fillBoxColor(object x) // ditto
{
mySolidColorBrush.Color = (Color)x; // need a cast here
rctIsConnected.Fill = mySolidColorBrush;
}

thanks for the reply vulpes, but i tried that and i'm back to the error that the thread can't access it because it's not the same thread

vulpes
17279

Well, that's a different error altogether but it makes no sense because, of course, the rationale for using Dispatcher.Invoke in the first place is to marshal UI changes back to the UI thread. Do you still get the error if you use BeginInvoke rather than Invoke?

when i try begininvoke, i get no errors, but it seems to just skip over the color change. tried to add a breakpoint, and it's definately running the line, but it's not changing the color.

vulpes
17279

It's difficult to know what else to suggest but I'd try reducing the DispatcherPriority from Send to Normal in case that's upsetting matters.

Feedback