pingu2k4
pingu2k4

Reputation: 1050

CollectionView does not support changes to its source collection from a thread different from the dispatcher thread - caused from dispatcher thread

I have an ObservableCollection, and an ICollectionView using the OC as a source:

private ObservableCollection<Comment> _Comments = new ObservableCollection<Comment>();
/// <summary>
/// Comments on the account
/// </summary>
[BsonElement("comments")]
public ObservableCollection<Comment> Comments
{
    get
    {
        return _Comments;
    }
    set
    {
        _Comments = value;
        OnPropertyChanged("Comments");
        OnPropertyChanged("CommentsSorted");
    }
}
private ICollectionView _CommentsSorted;
/// <summary>
/// Sorted list (reverse order) of the comments
/// </summary>
[BsonIgnore]
public ICollectionView CommentsSorted
{
    get
    {
        return _CommentsSorted;
    }
    set
    {
        _CommentsSorted = value;
        OnPropertyChanged("CommentsSorted");
    }
}

I have a command, which runs:

obj.Comments.Add(new Comment(Message));

where obj is an instance of the class containing the observable collection.

When calling this line, I am hitting the following exception:

System.NotSupportedException: 'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.'

I have opened the Debug > Windows > Threads panel, and it is running on the main thread. I have tried putting it inside App.Current.Dispatcher.Invoke(...), no luck.

I can't figure out why this is happening. To make things stranger, I am able to run this fine, no problems at all, on another instance of the same class, which was created at the same time (returned and created together from my database in the same call). The first one I added a comment to no problem, and still can every time, yet all the others I have tried fail.

Upvotes: 1

Views: 5838

Answers (2)

manit
manit

Reputation: 684

I had an issue where removing a item from an observable collection was throwing an error. "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread"

Dispatcher.Invoke(() =>
{
    ds.Remove(item);
});

My issue was calling this code from a different thread:

CollectionViewSource.GetDefaultView(ctlImagesGrid.ItemsSource).Refresh();

I assume, this forced the ItemsSource to be referenced from the calling thread. When I invoked this code from the main thread, modifying the observable collection had no errors.

 Dispatcher.Invoke(() =>
 {                         
  CollectionViewSource.GetDefaultView(ctlImagesGrid.ItemsSource).Refresh();
 });        

Upvotes: 0

Neil B
Neil B

Reputation: 2224

In my case the problem was that the collection view was refreshed in a task. Then later adding to the collection from the Main UI thread caused an exception.

When constructing the view model the collection was refreshed in a delayed task.

public MainVM()
{
    //other code...
    Task.Delay(100).ContinueWith(_ => UpdatePreferences());
}


public void UpdatePreferences()
{
    //other code..
    CollectionViewSource.GetDefaultView(Data.Customers).Refresh();
}

I was able to fix the problem by invoking the dispatcher.

Task.Delay(100).ContinueWith(_ => App.Current.Dispatcher.Invoke(()=> UpdatePreferences()));

Upvotes: 5

Related Questions