System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
I am adding/removing from an ObservableCollection
which is not on a UI thread.
I have a method names EnqueueReport
to add to the collection and a DequeueReport
to remove from the collection.
The flow of steps is as below:
EnqueueReport
whenever a new report is requestedObservableCollection
)DequeueReport
if the report is generatedI am not much in C# libraries. Can someone please guide me on this?
As of .net framwork 4.5 you can use native collection synchronization.
BindingOperations.EnableCollectionSynchronization(YourCollection, YourLockObject);
YourLockObject
is instance of any object e.g. new Object();
. Use one per collection.
This eliminates the need of some special class or anything. Just enable and enjoy ;)
[edit] As stated in the comments by Mark and Ed (thanks for clarifying!), this does not relieve you from locking the collection on updates as it just synchonizes the collection-view-binding and does not magically make the collection thread-safe itself. [/edit]
PS: BindingOperations
resides in Namespace System.Windows.Data
.
The solution Franck posted here will work in the case where one thread is adding things, but ObservableCollection itself (and List, which it's based on) are not thread-safe. If multiple threads are writing to the collection, hard-to-track-down bugs could be introduced. I wrote a version of ObservableCollection that uses a ReaderWriteLockSlim to be truly thread-safe.
Unfortunately, it hit the StackOverflow character limit, so here it is on PasteBin. This should work 100% with multiple readers/writers. Just like regular ObservableCollection, it's invalid to modify the collection in a callback from it (on the thread that received the callback).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With