title: C# WPF - Display texts using delays
body: <p><strong>Following case</strong></p>
  
  <p>Button pressed</p>
  
  <p>Showing Textbox "Successfully ordered"</p>
  
  <p>Delay 2 seconds still showing text</p>
  
  <p>Delay is over and text changes/disappears to "Place your chip"</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Using Thread Sleep it freezes whole UI before showing the Text and just the 2nd text appears after the delay. Interesting is, that a sound I play after changing the text is played but the text isn't shown.</p>
  
  <p>Using Task (async/delay/await) it doesn't wait for the delay and just shows both in a row, so first text for a really short time and then instantly changing to 2nd text.</p>
  
  <p>Using Task (wait) the program crashes.</p>
  
  <p>Using Timer it has the same effect as using Task (async or/and delayed).</p>
  
  <p>Using while (time, 2nd time with +2seconds) now just pushing new time into time and if time is over it's finished, but has the same effect as Thread Sleep - whole UI freezes.
  <hr>
  Maybe it's interesting to know, that there are timers running in the background the whole time.</p>
  
  <p>I tried many version I found here, but none did work - maybe it's about WPF, so I'm asking right now if someone has a solution for WPF delays.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>        private void SendWithDelay()
          {
            //  close(); // Sets text back to "place your chip" but for here:
            tbPlace.Text = "Place your chip";
          }
  
          private void changeVisibilityForDelay()
          {
                  // called by pressed button
                  tbPlace.Text = "Successfully ordered";
                  tbPlace.Visibility = Visibility.Visible;
                  Task.Delay(2000).ContinueWith(t =&gt; SendWithDelay(), TaskScheduler.FromCurrentSynchronizationContext());
          }
  </code></pre>