title: Dispatcher.BeginInvoke to different window
body: <p>I have <code>Window</code> let's call it <code>W</code> and class <code>LogicW</code>, at constructor of <code>W</code> i pass reference to that window to <code>LogicW</code></p>
  
  <pre><code>public W
  {
      InitializeComponent();
      LogicW l = new LogicW(this);
  }
  </code></pre>
  
  <p>And at <code>LogicW</code> I'm doing all the logic stuff, so now I whant to start new thread at W that will call method from <code>LogicW</code> do some work and add <code>UserControll</code> to <code>W</code>.</p>
  
  <pre><code>public void add()
  {
      Thread t = new Thread(() =&gt; l.Add());
      t.IsBackground = true;
      t.SetApartmentState(ApartmentState.STA);
      t.Start();
  }
  </code></pre>
  
  <p>And in <code>LogicW</code> add method:</p>
  
  <pre><code>public void Add()
  {
  ...
  Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =&gt; baseWindow.GroupsStackPanel.Children.Add(i)));
  }
  </code></pre>
  
  <p>Where:
  <code>baseWindow</code> is reference to W passed in LogicW constructor
  <code>GroupsStackPanel</code> is normal StackPanel at W
  <code>i</code> is UserControll</p>
  
  <p>But I get: </p>
  
  <pre><code>The calling thread cannot access this object because a different thread owns it.
  </code></pre>
  
  <p>I also tried to pass reference to that window in argument:</p>
  
  <pre><code>public void Add(MyWindow w)
  {
  ...
  Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =&gt; w.GroupsStackPanel.Children.Add(i)));
  }
  </code></pre>
  
  <p>How can I fix that?</p>