question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing collection in CollectionChanged event results in exception

See original GitHub issue

Description

If you change the collection within CollectionChanged event of an ObservableCollection an exception is thrown: Cannot change ObservableCollection during a CollectionChanged event.

Which at first makes sense and is probably by design to avoid ending in a loop it simply block us from making SAFE changes to the collection which dont end in a loop. Consider the following pseudo code:

var collection = new ObservableCollection<int>() { 0, 1, 2, 3, 4}

collection.CollectionChanged += (e, s) => {
	if (s.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) {		
		collection.Move(ACollection[1], 0);
	}
};
collection.Add(5);

This code is absolutely safe to execute. But an exception is thrown. The only way to get it to work is moving the modification to another thread:

var collection = new ObservableCollection<int>() { 0, 1, 2, 3, 4}

collection.CollectionChanged += (e, s) => {
	if (s.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) {		
		Bla();
	}
};
collection.Add(5);

async void Bla() {
	Application.Current.Dispatcher.Invoke(() => {	
		collection.Move(collection[1], 0);
	}, System.Windows.Threading.DispatcherPriority.Background);
}

I dont think changing it would be a breaking change because no existing code looks for and handle that specific exception. Instead of throwing an exception, a warning in VS analyzer might be better. Because an exception shouldn’t be used as a warning like it is now.

Reproduction Steps

See above

Expected behavior

No exception, at most a VS analyzer warning

Actual behavior

Exception is thrown

Regression?

No response

Known Workarounds

See above

Impact

No response

Configuration

.NET 6

Other information

No response

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Symbaicommented, Feb 27, 2023

I’ve created an issue in .NET runtime repo and linked it here. Closing this for now.

1reaction
lindexicommented, Feb 27, 2023

@Symbai I find the code of ObservableCollection is in dotnet runtime. Should you move the issue to dotnet runtime?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing collection from CollectionChanged event
1 Answer 1 ... The problem is that you are unsubscribing from the event within the event which has yet to complete. Drop...
Read more >
CollectionChanged event not being triggered?
I currently have a problem where something specific appears to be causing a change to an item collection. The code I am running...
Read more >
ObservableCollection<T> Class
This interface exposes the CollectionChanged event, an event that should be raised whenever the underlying collection changes.
Read more >
ObservableCollection<T>.CollectionChanged Event
Occurs when an item is added, removed, or moved, or the entire list is refreshed.
Read more >
Xamarin Forms Exception OnLoadMore - "Cannot change ...
When doing a "first load" of a datasource the DataGridView control invokes two times the LoadMore event. This causes the data to loaded...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found