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?
You can create a simple thread friendly version of the observable collection. Like the following :
public class MTObservableCollection<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler CollectionChanged = this.CollectionChanged;
if (CollectionChanged != null)
foreach (NotifyCollectionChangedEventHandler nh in CollectionChanged.GetInvocationList())
{
DispatcherObject dispObj = nh.Target as DispatcherObject;
if (dispObj != null)
{
Dispatcher dispatcher = dispObj.Dispatcher;
if (dispatcher != null && !dispatcher.CheckAccess())
{
dispatcher.BeginInvoke(
(Action)(() => nh.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
DispatcherPriority.DataBind);
continue;
}
}
nh.Invoke(this, e);
}
}
}
with that now do a massive find & replace and change all your ObservableCollection
to MTObservableCollection
and your good to go