I create a SolidColorBrush
on some non-GUI thread, and want to pass it to a GUI thread to display it, but I get InvalidOperationException
: The calling thread cannot access this object because a different thread owns it.
(even if I try to Freeze();
it). How do I pass object that was created in thread X to thread Y ?
I know I can create this SolidColorBrush
object in the GUI thread with Dispatcher
, but that would complicate everything... I want to create it in the worker thread.
Additional details:
I initialize some static delegate in some static class, to allow sending messages from business layer to GUI:
public static class Gui{
private static PrintMethodDelegate _printMethod;
public static void InitializeGuiInterface(PrintMethodDelegate printMethod){
_printMethod = printMethod;
}
public static void Print(GuiMessage data) { _printMethod(data); }
}
Initialization (in the GUI thread):
Gui.InitializeGuiInterface(_messagesToUserHandler.PrintMessage);
Then in another (non-gui) thread, I use it:
Gui.Print(new GuiMessage(testDescription) { Foreground = new SolidColorBrush(someColor) });
while GuiMessage
is:
public class GuiMessage {
public string Msg { get; set; }
private SolidColorBrush _foregroundBrush;
public SolidColorBrush Foreground
{
get { return _foregroundBrush; }
set { _foregroundBrush = value; }
}
}