Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload