title: WPF Dispatcher Thread Freeze main window
body: <p>Instead of working in the background - this code still freeze my program:</p>
  
  <pre><code>private void button_Click(object sender, RoutedEventArgs e)
  {
      this.Dispatcher.BeginInvoke(new Action(() =&gt;
      {
          Thread.Sleep(5000);
          label.Content = "Done";
      }), DispatcherPriority.Normal);
  }
  </code></pre>
  
  <p>I have tried with Thread/Tasks, thread example:</p>
  
  <pre><code>private void button_Click(object sender, RoutedEventArgs e)
  {
      var t = new Thread(new ThreadStart(runtask));
      t.Start();
  }
  
  private void runtask()
  {
      this.Dispatcher.BeginInvoke(new Action(() =&gt;
      {
          Thread.Sleep(5000);
          label.Content = "Done";
      }), DispatcherPriority.Normal);
  }
  </code></pre>
  
  <p>Task example:</p>
  
  <pre><code>private void button_Click(object sender, RoutedEventArgs e)
  {
      Task.Run(() =&gt;
      {
          Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =&gt;
          {
              Thread.Sleep(5000);
              label.Content = "Done";
          }));
      });
  }
  </code></pre>
  
  <p>And still my program is freezing. Any suggestions?</p>