title: using dispatcher to update UI from class with async methods in wpf
body: <p>I've got a class with manage data, and this class has a ObservableCollection which is bind in UI menu. The problem is that the observable collection loads data, but my UI does not show it.</p>
  
  <p>My class is like this</p>
  
  <pre><code>public class DAL : INotifyPropertyChanged
  {
      public DAL()
      {
          this.unity = new UnitOfWork(@"http://192.168.0.173/vocalcontactapi");
      }
  
      private UnitOfWork unity;
  
      public event PropertyChangedEventHandler PropertyChanged;
      private void OnPropertyChanged(string propertyName)
      {
          if (this.PropertyChanged != null)
              this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
  
      private ObservableCollection&lt;EstadosAgente&gt; estadosPausa = new ObservableCollection&lt;EstadosAgente&gt;();
      public ObservableCollection&lt;EstadosAgente&gt; EstadosPausa
      {
          get { return this.estadosPausa; }
      }
  
      public async Task&lt;bool&gt; GetAgentStatesAsync()
      {
          await awaitGetAgentStatesTask();
          OnPropertyChanged("EstadosPausa");
          return true;
      }
  
      private Task awaitGetAgentStatesTask()
      {
          //UnitOfWork unity = new UnitOfWork(Properties.Settings.Default.restServer);
          NameValueCollection parms = new NameValueCollection();
          parms.Add("servicioId", "1");
          return Task.Run(() =&gt;
          {
              try
              {
                  var estados = unity.EstadosAgente.GetAll(parms).Where(q =&gt; q.habilitado == true).Select(p =&gt; p).ToList();
                  if (estados == null)
                      return;
  
                  estados.ForEach(x =&gt;
                  {
                      Application.Current.Dispatcher.Invoke(new Action(() =&gt;
                      {
                          this.EstadosPausa.Add(x); //*** I think here is the problem***
                      }));
                  });
  
              }
              catch (Exception ex)
              {
                  string err = ex.Message;
              }
          });
      }
  
  }
  </code></pre>
  
  <p>And mainWindow I have a Property of class DAL:</p>
  
  <pre><code>private DAL data = new DAL();
  public DAL Data { get{ return this.data}}
  </code></pre>
  
  <p>In my menu I've got next:</p>
  
  <pre><code>  &lt;Menu Grid.Row="1"&gt;
              &lt;MenuItem Header="uno" ItemsSource="{Binding DAL.EstadosPausa}" Click="DataBoundMenuItem_Click"&gt;
                  &lt;MenuItem.ItemContainerStyle&gt;
                      &lt;Style TargetType="MenuItem"&gt;
                          &lt;Setter Property="Header" Value="{Binding estado}"/&gt;
                          &lt;Setter Property="Tag" Value="{Binding}" /&gt;
                      &lt;/Style&gt;
                  &lt;/MenuItem.ItemContainerStyle&gt;
              &lt;/MenuItem&gt;
          &lt;/Menu&gt;
  </code></pre>
  
  <p>Obviously, all properties of the loaded data are correct. Any help please?</p>