title: How to add a delay to a WPF program without blocking the UI
body: <p>I am building a device emulator. When it starts, it takes some time for it to initialized. This would be logically represented by being turned on and going immediately to an "Initialization" state, and after some time it goes to "Ready" state.</p>
  
  <p>I am using MVVM, so the ViewModel will for now represent all the device logic. Each of the possible states have a datatriggered style to be rendered by the View. If I just set the state when I build the viewmodel, the view renders with the correct appearance.</p>
  
  <p>What I want to do is to create a "timeout state", that is, when some event occurs (starting the application, clicking a certain button), the device enters a state for a fixed time, and then falls back to the "ready", or "idle" state.</p>
  
  <p>I thought about using Sleep, but sleep blocks the UI (so they say). So I think about using Threads, but I am not sure how to do it. This is what I have got so far:</p>
  
  <pre><code>using System.ComponentModel;
  
  namespace EmuladorMiotool {
      public class MiotoolViewModel : INotifyPropertyChanged {
          Estados _estado;
  
          public Estados Estado {
            get {
                return _estado;
            }
            set {
                _estado = value;
                switch (_estado) {
                    case Estados.WirelessProcurando:
                        // WAIT FOR TWO SECONDS WITHOUT BLOCKING GUI
                        // It should look like the device is actually doing something
                        // (but not indeed, for now)
                        _estado = Estados.WirelessConectado;
                        break;
                }
                RaisePropertyChanged("Estado");
            }
          }
  
          public MiotoolViewModel() {
              // The constructor sets the initial state to "Searching"
              Estado = Estados.WirelessProcurando;
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          protected virtual void RaisePropertyChanged(string propertyName) {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (handler != null)
                  handler(this, new PropertyChangedEventArgs(propertyName));
          }
  
      }
  
      public enum Estados {
          UsbOcioso,
          UsbAquisitando,
          UsbTransferindo,
          WirelessNãoPareado,
          WirelessPareado,
          WirelessDesconectado,
          WirelessProcurando,
          WirelessConectado,
          WirelessAquisitando,
          DataLoggerOcioso,
          DataLoggerAquisitando,
          Erro,
          Formatando
      }
  }
  </code></pre>