title: INotifyPropertyChanged issue, property is not being updated from Dispatcher
body: <p>I have an issue with some of my properties not being updated when I use a <code>Dispatcher</code> from a separate thread.  When I check for values in the array, they're all 0's. </p>
  
  <p>Properties:</p>
  
  <pre><code>private string[] _years;
  public string[] Years
  {
      get { return _years; }
      private set
      {
          if (_years == value)
      {
          return;
      }
  
      _years = value;
      OnPropertyChanged("Years");
      }
  }
  
  private int[] _yearCounts;
  public int[] YearCounts
  {
      get { return _yearCounts; }
      private set
      {
          if (_yearCounts == value)
          {
              return;
          }
  
          _yearCounts = value;
          OnPropertyChanged("YearCounts");
      }
  }
  
  private ObservableCollection&lt;RecordModel&gt; _missingCollection;
  public ObservableCollection&lt;RecordModel&gt; MissingCollection
  {
      get { return _missingCollection; }
      private set
      {
          if (_missingCollection == value)
          {
              return;
          }
  
          _missingCollection = value;
          OnPropertyChanged("MissingCollection");
      }
  }
  </code></pre>
  
  <p>Constructor:</p>
  
  <pre><code>public MissingReportsViewModel()
  {
      YearCounts = new int[4];
      Years = new string[4];
      Initialize();
  }
  </code></pre>
  
  <p>Methods:</p>
  
  <pre><code>private void Initialize()
  {
      SetYears();
      Task task = new Task(() =&gt; 
      { 
          MissingCollection = new AccessWorker().GetMissingReports(); 
      });
      task.ContinueWith((result) =&gt; 
      { 
          SetYearCounts(); 
      });
      task.Start();
  }
  
  private void SetYears()
  {
      for (int i = 0; i &lt; 4; i++)
      {
          Years[i] = DateTime.Now.AddYears(-i).Year.ToString();
      }
  }
  
  private void SetYearCounts()
  {
      for (int i = 0; i &lt; 4; i++)
      {
          int n = i;
          Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
              new Action(() =&gt; YearCounts[n] = MissingCollection.Where(item =&gt;
                  item.RepNum.Substring(0, 4).Equals(Years[n])).ToList().Count()));
      }
  }
  </code></pre>
  
  <p>INotifyPropertyChanged:</p>
  
  <pre><code>public event PropertyChangedEventHandler PropertyChanged;
  
  private void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }
  </code></pre>
  
  <p>The problem is that after this is ran, each index of <code>YearsCount</code> is set at <code>0</code>.  If I get rid of Task.Run() and <code>Dispatcher</code>, the program freezes up a bit during the lengthy operation, but everything is displayed properly.  So, I'm messing up somewhere and failing to properly update the <code>YearsCount</code> property.</p>
  
  <p><strong>EDIT (SOLUTION?):</strong></p>
  
  <p>Huge thanks to Garry Vass, who hanged in there through two of my lengthy questions, Jon Skeet (from the first post), and Shoe.  I was able to get it up and running without any issues like this:</p>
  
  <p><strong>XAML</strong></p>
  
  <p>Four years:</p>
  
  <pre><code>&lt;TextBlock TextWrapping="Wrap" 
             Style="{DynamicResource SectionBodyTextStyle}"&gt;
      &lt;Run Text="{Binding YearlyStats[0].Year}"/&gt;
      &lt;Run Text=":"/&gt;
  &lt;/TextBlock&gt;
  &lt;TextBlock TextWrapping="Wrap" Grid.Row="1" 
             Style="{DynamicResource SectionBodyTextStyle}"&gt;
      &lt;Run Text="{Binding YearlyStats[1].Year}"/&gt;
      &lt;Run Text=":"/&gt;
  &lt;/TextBlock&gt;
  &lt;TextBlock TextWrapping="Wrap" Grid.Row="2" 
             Style="{DynamicResource SectionBodyTextStyle}"&gt;
      &lt;Run Text="{Binding YearlyStats[2].Year}"/&gt;
      &lt;Run Text=":"/&gt;
  &lt;/TextBlock&gt;
  &lt;TextBlock TextWrapping="Wrap" Grid.Row="3" 
             Style="{DynamicResource SectionBodyTextStyle}"&gt;
      &lt;Run Text="{Binding YearlyStats[3].Year}"/&gt;
      &lt;Run Text=":"/&gt;
  &lt;/TextBlock&gt;
  </code></pre>
  
  <p>Four stats (one per year):</p>
  
  <pre><code>&lt;TextBlock Text="{Binding YearlyStats[0].Count}" Grid.Column="1" 
             Margin="10,0,0,0"/&gt;
  &lt;TextBlock Text="{Binding YearlyStats[1].Count}" Grid.Column="1" Grid.Row="1" 
             Margin="10,0,0,0"/&gt;
  &lt;TextBlock Text="{Binding YearlyStats[2].Count}" Grid.Column="1" Grid.Row="2" 
             Margin="10,0,0,0"/&gt;
  &lt;TextBlock Text="{Binding YearlyStats[3].Count}" Grid.Column="1" Grid.Row="3" 
             Margin="10,0,0,0"/&gt;
  </code></pre>
  
  <p><strong>C# Code</strong></p>
  
  <p>Data Object:</p>
  
  <pre><code>public class MissingReportInfoModel : INotifyPropertyChanged
  {
      private string _year;
      public string Year
      {
          get { return _year; }
          set
          {
              if (_year == value)
              {
                  return;
              }
  
              _year = value;
              OnPropertyChanged("Year");
          }
      }
  
      private int _count;
      public int Count
      {
          get { return _count; }
          set
          {
              if (_count == value)
              {
                  return;
              }
  
              _count = value;
              OnPropertyChanged("Count");
          }
      }
  
      public MissingReportInfoModel()
      {
          Year = "Not Set";
          Count = 0;
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }
  </code></pre>
  
  <p>ViewModel Properties:</p>
  
  <pre><code>private ObservableCollection&lt;MissingReportInfoModel&gt; _yearlyStats;
  public ObservableCollection&lt;MissingReportInfoModel&gt; YearlyStats
  {
      get { return _yearlyStats; }
      private set
      {
          if (_yearlyStats == value)
          {
              return;
          }
  
          _yearlyStats = value;
          OnPropertyChanged("YearlyStats");
      }
  }
  
  private ObservableCollection&lt;RecordModel&gt; _missingCollection;
  public ObservableCollection&lt;RecordModel&gt; MissingCollection
  {
      get { return _missingCollection; }
      private set
      {
          if (_missingCollection == value)
          {
              return;
          }
  
          _missingCollection = value;
          OnPropertyChanged("MissingCollection");
      }
  }
  </code></pre>
  
  <p>ViewModel Constructor:</p>
  
  <pre><code>public MissingReportsViewModel()
  {
      YearlyStats = new ObservableCollection&lt;MissingReportInfoModel&gt;();
      Initialize();
  }
  </code></pre>
  
  <p>ViewModel Methods:</p>
  
  <pre><code>private void Initialize()
  {
      SetYears();
      Task task = new Task(() =&gt; 
      { 
          MissingCollection = new AccessWorker().GetMissingReports(); 
      });
      task.ContinueWith((result) =&gt; 
      { 
          SetCounts(); 
      });
      task.Start();
  }
  
  private void SetYears()
  {
      for (int i = 0; i &lt; 4; i++)
      {
          var info = new MissingReportInfoModel();
          info.Year = DateTime.Now.AddYears(-i).Year.ToString();
          YearlyStats.Add(info);
      }
  }
  
  private void SetCounts()
  {
      for (int i = 0; i &lt; 4; i++)
      {
          int n = i;
          Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
              new Action(() =&gt; 
              {
                  YearlyStats[n].Count = MissingCollection.Where(item =&gt;
                      item.RepNum.Substring(0, 4).Equals(YearlyStats[n].Year)).ToList().Count();
              }));
      }
  }
  </code></pre>