title: c# wpf dispatcher.beginInvoke freeze
body: <p>im working on an application which communicates with server.</p>
  
  <pre><code>thread  = new Thread(new ThreadStart(ClientStart));
  thread.SetApartmentState(ApartmentState.STA);                
  thread.Start();
  
  private void ClientStart()
      {
          tcpClient = new TcpClient(ip,3000);
          stream = tcpClient.GetStream();
          while (true) {                
              stream.Read(...);
              PrintMessage(...);
          .....
          }
  }
  private void PrintMessage(LogWin w, string msg)
  {
    DateTime dt = DateTime.Now;
    w.GetMessageBox().Dispatcher.BeginInvoke(DispatcherPriority.Input,new Action(()=&gt;w.GetMessageBox()
              .AppendText(String.Format("{0:d/M/yyyy HH:mm:ss}", dt) + " : " + msg)));
  
  }
  </code></pre>
  
  <p>later i need to print result to a messagebox. I know i have to use Dispatcher, because im working in another thread, but my application freezes even if i use beginInvoke method.</p>
  
  <p><strong>Edit based on Sheridan's answer</strong>:</p>
  
  <p>now im getting:
  An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll</p>
  
  <p>Additional information: The calling thread cannot access this object because a different thread owns it.</p>
  
  <pre><code>Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
  thread = new Thread(() =&gt; ClientStart(dispatcher));
  thread.SetApartmentState(ApartmentState.STA);
  thread.Start();
  
  private void ClientStart(Dispatcher dispatcher)
      { 
  ....
  </code></pre>
  
  <p>and changed print method:</p>
  
  <pre><code> dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =&gt;
         w.GetMessageBox()
              .AppendText(String.Format("{0:d/M/yyyy HH:mm:ss}", dt) + " : " + msg)));
  </code></pre>
  
  <p><strong>solved</strong> by using <code>Application.Current.Dispatcher</code> </p>