title: C# Simple Countdown - What am I doing wrong?
body: <p>I wanted to make a simple Countdown-Application with C# to show as an example.</p>
  
  <p>For the very first and basic version I use a Label to display the current time left in seconds and a Button to start the countdown. The Button's Click-Event is implemented like this:</p>
  
  <pre><code>private void ButtonStart_Click(object sender, RoutedEventArgs e)
      {
          _time = 60;
          while (_time &gt; 0)
          {
              _time--;
              this.labelTime.Content = _time + "s";
              System.Threading.Thread.Sleep(1000);
          }
      }
  </code></pre>
  
  <p>Now when the user clicks the Button the time is actually counted down (as the application freezes (due to Sleep())) for the chosen amount of time but the Label's context is not refreshed.</p>
  
  <p>Am I doing something generally wrong (when it comes to Threads) or is it just a problem with the UI?</p>
  
  <hr>
  
  <p>Thank you for your answers!
  I now use a System.Windows.Threading.DispatcherTimer to do as you told me. Everything works fine so this question is officially answered ;)</p>
  
  <p>For those who are interested: Here is my code (the essential parts)</p>
  
  <pre><code>public partial class WindowCountdown : Window
  {
      private int _time;
      private DispatcherTimer _countdownTimer;
  
      public WindowCountdown()
      {
          InitializeComponent();
          _countdownTimer = new DispatcherTimer();
          _countdownTimer.Interval = new TimeSpan(0,0,1);
          _countdownTimer.Tick += new EventHandler(CountdownTimerStep);
  
      }
  
      private void ButtonStart_Click(object sender, RoutedEventArgs e)
      {
          _time = 10;
          _countdownTimer.Start();
  
      }
  
      private void CountdownTimerStep(object sender, EventArgs e)
      {
          if (_time &gt; 0)
          {
              _time--;
              this.labelTime.Content = _time + "s";
          }
          else
              _countdownTimer.Stop();
      }
  }
  </code></pre>