title: "The calling thread must be STA" workaround
body: <p>I know there are a few answers on this topic on SO, but I can not get any of the solutions working for me. I am trying to open a new window, from an ICommand fired from within a datatemplate. Both of the following give the aforementioned error <strong>when the new window is instantiated</strong> (within "new MessageWindowP"): </p>
  
  <p><strong>Using TPL/FromCurrentSynchronizationContext</strong> <em>Update: works</em></p>
  
  <pre><code>public class ChatUserCommand : ICommand
  {
      public void Execute(object sender)
      {
          if (sender is UserC)
          {
              var user = (UserC)sender;
              var scheduler = TaskScheduler.FromCurrentSynchronizationContext();                  
              Task.Factory.StartNew(new Action&lt;object&gt;(CreateMessageWindow), user,CancellationToken.None, TaskCreationOptions.None,scheduler);         
          }
      }
  
      private void CreateMessageWindow(object o)
      {
          var user = (UserC)o;
          var messageP = new MessageWindowP();
          messageP.ViewModel.Participants.Add(user);
          messageP.View.Show();
      }
  }
  </code></pre>
  
  <p><strong>Using ThreadStart:</strong> <em>Update: not recommended, see Jon's answer</em></p>
  
  <pre><code>public class ChatUserCommand : ICommand
  {
      public void Execute(object sender)
      {
          if (sender is UserC)
          {
              var user = (UserC)sender;
  
              var t = new ParameterizedThreadStart(CreateMessageWindow);
              var thread = new Thread(t);
              thread.SetApartmentState(ApartmentState.STA);
              thread.Start(sender);           
          }
      }
  
      private void CreateMessageWindow(object o)
      {
          var user = (UserC)o;
          var messageP = new MessageWindowP();
          messageP.ViewModel.Participants.Add(user);
          messageP.View.Show();
      }
  }
  </code></pre>
  
  <p>Thanks</p>
  
  <p>EDIT. Based on the responses so far, I'd like to point out that I have also tried BeginInvoke on the current dispatcher, as well as executing the code in the original method (that's how the code started). See below:</p>
  
  <p><strong>BeginInvoke</strong> <em>Update: not recommended see Jon's answer</em></p>
  
  <pre><code>public class ChatUserCommand : ICommand
  {
      public void Execute(object sender)
      {
          if (sender is UserC)
          {
              var user = (UserC)sender;
              Dispatcher.CurrentDispatcher.BeginInvoke(new Action&lt;object&gt;(CreateMessageWindow), sender);       
          }
      }
  
      private void CreateMessageWindow(object o)
      {
          var user = (UserC)o;
          var messageP = new MessageWindowP();
          messageP.ViewModel.Participants.Add(user);
          messageP.View.Show();
      }
  }
  </code></pre>
  
  <p><strong>In same thread</strong> <em>Update: works if you are on UI thread already</em></p>
  
  <pre><code>public class ChatUserCommand : ICommand
  {
      public void Execute(object sender)
      {
          if (sender is UserC)
          {
              var user = (UserC)sender;
              var messageP = new MessageWindowP();
              messageP.ViewModel.Participants.Add(user);
              messageP.View.Show();    
          }
      }
  
  }
  </code></pre>
  
  <p><strong>BeginInvoke, using reference to dispatcher of first/main window</strong> <em>Update: works</em></p>
  
  <pre><code> public void Execute(object sender)
     {
         if (sender is UserC)
         {
              var user = (UserC)sender;
                      GeneralManager.MainDispatcher.BeginInvoke(
                                 DispatcherPriority.Normal,
                                 new Action(() =&gt; this.CreateMessageWindow(user)));      
          }
      }
  </code></pre>
  
  <p>where GeneralManager.MainDispatcher is a reference to the Dispatcher of the first window I create:</p>
  
  <pre><code>     [somewhere far far away]
          mainP = new MainP();
          MainDispatcher = mainP.View.Dispatcher;
  </code></pre>
  
  <p>I'm at a loss.</p>