16

I have a CollectionChanged event that is tied to an ObservableCollection. This CollectionChanged event then calls another function which is intended to update another collection (of the same type) with an item from the original collection. I have read other posts on the "Cannot change ObservableCollection during a CollectionChanged event" and I understand completely why it is frowned upon to modify a collection inside of a CollectionChanged event and why this can result in a circular reference... but in this particular case I am not modifying the original collection, I'm only adding an Item from it to an unrelated collection.

So to my question.. is there anything wrong with what I am doing? I don't see how adding an item from the collection with the event tied to it to another collection would fire the event again and create a circular reference (although please tell me if I'm wrong about this).

Also... is there any way around it? I read several posts advising to run this on a separate thread but when I try that I get the following error instead.

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

I'm really just after a better understanding of what's going on here. Any advice would be much appreciated.

Edit

Simplified example as requested

void originalCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    update(originalCollection);
}

private void update(object parameter)
{
    foreach (var originalCollectionItem in parameter)
        newCollection.Add(originalCollectionItem);
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
aw04
  • 10,857
  • 10
  • 56
  • 89
  • What error are you getting? – SLaks Jun 06 '13 at 17:47
  • The title of the post is the error message.. Cannot change ObservableCollection during a CollectionChanged event – aw04 Jun 06 '13 at 17:48
  • Could you post the actual code? – Tom Studee Jun 06 '13 at 17:49
  • But if you are passing Coverages as the parameter to getCoverages then you are modifying THE collection in the foreach. Coverages.Add. – paparazzo Jun 06 '13 at 18:25
  • The naming is the same in both but the original Coverages is the parameter in getCoverages... not the collection that is Coverages.Add() (I do realize how confusing that sounds as I type it sorry) – aw04 Jun 06 '13 at 18:29
  • Then I recommend you break this down to a simple example that reproduces the problem and use unique names. – paparazzo Jun 06 '13 at 18:48

1 Answers1

7

If you are using WindowsForms just make sure to use the Invoke method on the Form to keep the executing code on the Dispatcher's thread.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.invoke.aspx

Or, if using WPF use the Dispatcher class.

http://dotnetpattern.com/wpf-dispatcher

Roberto Hernandez
  • 2,397
  • 1
  • 16
  • 18
  • The second link appears to be broken. I'm redirected to https://msdn.microsoft.com/magazine/msdn-magazine-issues which contains no information about the problem or the proposed solution – Blueriver Nov 07 '18 at 19:38
  • 1
    @Blueriver I updated the link, it doesn't match the original but it should have the information necessary. – Roberto Hernandez Nov 07 '18 at 20:28