title: WPF MVVM: Async method does not always update the UI
body: <p>I bound a button to a command in the view model that triggers a long-running operation. The operation is run in a separate task. Before and after the task, a property is set to reflect it running in the UI.</p>
  
  <p>My problem now is: Sometimes, when I click the button, the change of the property BEFORE the long operation runs is registered (i.e., the UI changes accordingly), but when the task finishes, it is not immediately. I have to click somewhere in the UI to make the change to be reflected.</p>
  
  <p>Here is how I did it (using MVVM Light):</p>
  
  <pre><code>_longRunningOpCommand = new RelayCommand(async () =&gt; await DoLongRunningThingAsync(), CanDoLongRunningThing);
  
  // ...
  
  public ICommand LongRunningOpCommand { get { return _longRunningOpCommand; } }
  
  // ...
  
  private async Task DoLongRunningThingAsync() {
      try {
          IsDoingStuff = true; // triggers a PropertyChangeEvent, is bound to UI
          await Task.Factory.StartNew(async () =&gt; {
              await _something.DoLenghtyOperation();
          }, TaskCreationOptions.LongRunning);
      } finally {
          IsDoingStuff = false;
      }
  }
  </code></pre>
  
  <p>Now, the UI elements update as soon as <code>IsDoingStuff</code> is becoming <code>false</code> as they should, but they are not updated when <code>IsDoingStuff</code> is becoming <code>true</code>. I have to click into the UI to have them updated.</p>
  
  <p>I think I'm doing something wrong, but can't track down what.</p>