Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an ObservableCollection from another thread

I've been trying to get a handle on the Rx library and working it out in WPF with MVVM. I broken my application into components such as repository and the ViewModel. My repository in able to provide a Collection of Students one by one but the moment i try to add into the View bound ObservableCollection it throws a thread error. I 'd apreciate some pointer on to get this working for me.

like image 751
Arshad Badar Khan Avatar asked Feb 20 '12 16:02

Arshad Badar Khan


1 Answers

You need to set the synchronization context correctly using

ObserveOn(SynchronizationContext.Current)

See this blog post

http://10rem.net/blog/2011/02/17/asynchronous-web-and-network-calls-on-the-client-in-wpf-and-silverlight-and-net-in-general

for an example.

Here's an example that works for me:

<Page.Resources>
    <ViewModel:ReactiveListViewModel x:Key="model"/>
</Page.Resources>

<Grid DataContext="{StaticResource model}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button Content="Start" Command="{Binding StartCommand}"/>
    <ListBox ItemsSource="{Binding Items}" Grid.Row="1"/>
</Grid>

public class ReactiveListViewModel : ViewModelBase
{
    public ReactiveListViewModel()
    {
        Items = new ObservableCollection<long>();
        StartCommand = new RelayCommand(Start);
    }

    public ICommand StartCommand { get; private set; }

    private void Start()
    {
        var observable = Observable.Interval(TimeSpan.FromSeconds(1));

        //Exception: This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
        //observable.Subscribe(num => Items.Add(num));

        // Works fine
        observable.ObserveOn(SynchronizationContext.Current).Subscribe(num => Items.Add(num));

        // Works fine
        //observable.ObserveOnDispatcher().Subscribe(num => Items.Add(num));
    }

    public ObservableCollection<long> Items { get; private set; }
}
like image 175
Phil Avatar answered Nov 06 '22 21:11

Phil