A few days ago I created this thread because I was unable to update an ObservableCollection from another thread. This was the solution from the thread:
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate
{
TheTVDB theTvdb = new TheTVDB();
foreach (TVSeries tvSeries in theTvdb.SearchSeries("Dexter"))
{
this.Overview.Add(tvSeries);
}
}),
DispatcherPriority.Background);
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 theTvdb.SearchSeries()
). Then I will iterate over the results and add those to my ObservableCollection
and this must happen on the UI thread.
Does this approach sound right?
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.
Thread thread = new Thread(new ThreadStart(delegate
{
TheTVDB theTvdb = new TheTVDB();
List<TVSeries> 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();
The above does not produce any error. Instead nothing happens. The UI does not freeze but it is not updated. The objects in Overview
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 ObservableCollection
on another thread.
Another solution I have tried is to use the MTObservableCollection from this answer to a similar question. When using that subclass of the ObservableCollection
, I did not dispatch anything myself. This gave me the following error:
Must create DependencySource on same Thread as the DependencyObject.
Can anybody please tell me how I can:
- Load something on a separate thread
- Use the results from step 1 to update an ObservableCollection which is bound to a listview
- Have the results shown in the UI without the UI freezing
I hope you can help me further.
UPDATE:
<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">
<UserControl.Resources>
<DataTemplate x:Key="CoverTemplate">
<StackPanel Orientation="Horizontal">
<Image Width="82" Height="85" Stretch="Fill" Source="{Binding Cover}" Margin="10,10,0,10"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#515050">
<Grid.Resources>
<ResourceDictionary>
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="#282828" />
<Setter Property="Margin" Value="0,0,0,5" />
<Setter Property="Padding" Value="0" />
</Style>
</ResourceDictionary>
</Grid.Resources>
<ListView Height="112"
Width="488"
Margin="12,150,12,218"
Foreground="#ffffff"
Background="#515050"
VerticalContentAlignment="Center"
BorderThickness="0"
ItemTemplate="{StaticResource CoverTemplate}"
ItemsSource="{Binding Overview}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<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}">
<ListView.Resources>
<ResourceDictionary>
<Style x:Key="hiddenStyle" TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</ResourceDictionary>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Cover" Width="auto" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=Cover}" Height="50" Margin="-6,0,0,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Title" Width="200" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Year" Width="100" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DisplayYear}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Button" Width="135" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Details" Width="100" Height="20" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>