title: Using ObservableCollection on a new thread
body: <p>A few days ago I created <a href="https://stackoverflow.com/questions/13192247/view-is-not-updated-when-on-a-separate-thread">this thread</a> because I was unable to update an ObservableCollection from another thread.
  This was the solution from the thread:</p>
  
  <pre><code>Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate
  {
      TheTVDB theTvdb = new TheTVDB();
      foreach (TVSeries tvSeries in theTvdb.SearchSeries("Dexter"))
      {
          this.Overview.Add(tvSeries);
      }
  }),
  DispatcherPriority.Background);
  </code></pre>
  
  <p>However, it seems that this is not really the solution as the UI still freezes while doing executing the delegate. My guess is that the above does not really run anything on another thread but instead dispatches it all to the UI thread.
  So what I really want to do is to create a new thread myself and do the loading (this happens in <code>theTvdb.SearchSeries()</code>). Then I will iterate over the results and add those to my <code>ObservableCollection</code> and this must happen on the UI thread.</p>
  
  <p>Does this approach sound right?</p>
  
  <p>I came up with the below which I thought would load the results and add those to the ObervableCollection and show them in my list view without the UI freezing.</p>
  
  <pre><code>Thread thread = new Thread(new ThreadStart(delegate
  {
      TheTVDB theTvdb = new TheTVDB();
      List&lt;TVSeries&gt; dexter = theTvdb.SearchSeries("Dexter");
  
      foreach (TVSeries tvSeries in dexter)
      {
          Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate
          {
              this.Overview.Add(tvSeries);
          }),
          DispatcherPriority.Normal);
      }
  }));
  thread.SetApartmentState(ApartmentState.STA);
  thread.Start();
  </code></pre>
  
  <p>The above does not produce any error. Instead nothing happens. The UI does not freeze but it is not updated. The objects in <code>Overview</code> is not presented in the UI and I have tested that the binding is correct. The objects will show correctly if I don't load them and add them to the <code>ObservableCollection</code> on another thread.</p>
  
  <p>Another solution I have tried is to use the <a href="https://stackoverflow.com/a/2104705/486845">MTObservableCollection from this answer</a> to a similar question. When using that subclass of the <code>ObservableCollection</code>, I did not dispatch anything myself. This gave me the following error:</p>
  
  <blockquote>
    <p>Must create DependencySource on same Thread as the DependencyObject.</p>
  </blockquote>
  
  <p>Can anybody please tell me how I can:</p>
  
  <ol>
  <li>Load something on a separate thread</li>
  <li>Use the results from step 1 to update an ObservableCollection which is bound to a listview</li>
  <li>Have the results shown in the UI without the UI freezing</li>
  </ol>
  
  <p>I hope you can help me further.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:acb="clr-namespace:AttachedCommandBehavior"
      mc:Ignorable="d"
      x:Class="TVSeriesLibrary.OverviewView"
      x:Name="UserControl"
      d:DesignWidth="512"
      d:DesignHeight="480"&gt;
  
      &lt;UserControl.Resources&gt;
          &lt;DataTemplate x:Key="CoverTemplate"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;Image Width="82" Height="85" Stretch="Fill" Source="{Binding Cover}" Margin="10,10,0,10"/&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid x:Name="LayoutRoot" Background="#515050"&gt;
          &lt;Grid.Resources&gt;
              &lt;ResourceDictionary&gt;
                  &lt;Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}"&gt;
                      &lt;Setter Property="Background" Value="#282828" /&gt;
                      &lt;Setter Property="Margin" Value="0,0,0,5" /&gt;
                      &lt;Setter Property="Padding" Value="0" /&gt;
                  &lt;/Style&gt;
              &lt;/ResourceDictionary&gt;
          &lt;/Grid.Resources&gt;
  
          &lt;ListView Height="112"
                    Width="488"
                    Margin="12,150,12,218"
                    Foreground="#ffffff"
                    Background="#515050"
                    VerticalContentAlignment="Center"
                    BorderThickness="0"
                    ItemTemplate="{StaticResource CoverTemplate}"
                    ItemsSource="{Binding Overview}"&gt;
              &lt;ListView.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;WrapPanel Orientation="Horizontal" /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListView.ItemsPanel&gt;
          &lt;/ListView&gt;
  
          &lt;ListView Height="170"
                    Margin="10,298,10,0"
                    VerticalAlignment="Center"
                    Foreground="#ffffff"
                    Background="#515050"
                    VerticalContentAlignment="Center"
                    BorderThickness="0"
                    Width="488" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ItemsSource="{Binding Path=Overview}"
                    SelectedItem="{Binding Path=SelectedTVSeries}"
                    ItemContainerStyle="{StaticResource ItemContStyle}"&gt;
              &lt;ListView.Resources&gt;
                  &lt;ResourceDictionary&gt;
                      &lt;Style x:Key="hiddenStyle" TargetType="GridViewColumnHeader"&gt;
                          &lt;Setter Property="Visibility" Value="Collapsed"/&gt;
                      &lt;/Style&gt;
                  &lt;/ResourceDictionary&gt;
              &lt;/ListView.Resources&gt;
              &lt;ListView.View&gt;
                  &lt;GridView&gt;
                      &lt;GridViewColumn Header="Cover" Width="auto" HeaderContainerStyle="{StaticResource hiddenStyle}"&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;Image Source="{Binding Path=Cover}" Height="50" Margin="-6,0,0,0" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
  
  
                      &lt;GridViewColumn Header="Title" Width="200" HeaderContainerStyle="{StaticResource hiddenStyle}"&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="{Binding Path=Name}" FontWeight="Bold"&gt;&lt;/TextBlock&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
  
                      &lt;GridViewColumn Header="Year" Width="100" HeaderContainerStyle="{StaticResource hiddenStyle}"&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="{Binding Path=DisplayYear}"&gt;&lt;/TextBlock&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
  
                      &lt;GridViewColumn Header="Button" Width="135" HeaderContainerStyle="{StaticResource hiddenStyle}"&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;Button Content="Details" Width="100" Height="20" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
  
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
          &lt;/ListView&gt;
      &lt;/Grid&gt;
  
  &lt;/UserControl&gt;
  </code></pre>