I have a Property of type Observable collection which returns another property collection.When i am enumerating the property from another class by accessing it, I get collection was modified exception.I tried taking lock on the property but it does not seems to work.Any help appreciated
I have a Property of type Observable collection which returns another property collection.When i
Share
Taking a lock on an object won’t do anything unless someone else also takes a lock on the same object. If you absolutely must access this collection from a background thread then you should make sure that both the thread enumerating the collection and the thread modifying the collection have locks on the same objecs.
Its also considered good practice to lock on dedicated locking objects rather than on publicly accessible objects, e.g.
If you are writing a GUI application then generally it is better to only modify the collection on the UI thread – if you need to do some background processing on the collection then consider taking a copy of the collection (e.g. an array) on the UI thread which the background thread then does its processing with.