title: Problems with multi-threading windows in WPF
body: <p>I'm opening av new WPF-window in an new thread like <a href="http://www.c-sharpcorner.com/uploadfile/suchit_84/creating-wpf-windows-on-dedicated-threads/" rel="nofollow">this</a>:</p>
  
  <pre><code>private void OnCreateNewWindow(object sender, RoutedEventArgs e)
  {
      Thread thread = new Thread(() =&gt;
      {
          Window1 w = new Window1();
          w.Show();
          Dispatcher.Run(); 
      });
  
      thread.SetApartmentState(ApartmentState.STA);
      thread.Start();
      }
  }
  </code></pre>
  
  <p>The problem is it gets stuck on the Show-method (without showing a window) and never reaches Dispatcher.Run.</p>
  
  <p>When checking the threads and call-stacks I can see two threads, Main and my new window-thread. The callstack of the window-thread is at the following frame:</p>
  
  <blockquote>
    <p>PresentationCore.dll!System.Windows.Input.CommandManager.FindCommandBinding(object
    sender, System.Windows.RoutedEventArgs e,
    System.Windows.Input.ICommand command, bool execute) + 0x1c0 bytes</p>
  </blockquote>
  
  <p>When trying modifications to the code I once managed to get an exception with the message:</p>
  
  <blockquote>
    <p>Cannot use a DependencyObject that belongs to a different thread than
    its parent Freezable.</p>
  </blockquote>
  
  <p>I get a feeling this has something to do with access to resources owned by the main thread that's not frozen. In this app I have a lot of generic styles and resources, brushes etc but I'm trying to avoid using them in this specific dialog by putting the following in the constructor of the window:</p>
  
  <blockquote>
    <p>InheritanceBehavior = InheritanceBehavior.SkipAllNow;</p>
  </blockquote>
  
  <p>But this does not seem to be enough. Somehow, some unallowed resource seem to be accessed anyway.</p>
  
  <p>The frame with FindCommandBinding hints that it could be a problem with a command-binding. But this dialog has no command-bindings.</p>
  
  <p>Are resources like brushes implicitly frozen if they are defined in a resource-dictionary? If not, how do I freeze them explicitly in a simple way? There are hundreds of brushes. But this may not be the problem here.</p>
  
  <p>Any other ideas?</p>