BlockReentrancy in ObservableCollection<T>
An ObservableCollection
implements INotifyCollectionChanged
and so it has a CollectionChanged
event. If there is a subscriber to this event, they could further modify the collection while the collection is already in the process of notification. Since the CollectionChanged
event keeps track of exactly what changed, this interaction can get very messy.
As a result, the ObservableCollection
allows, as a special case, a single subscriber of the CollectionChanged
event to modify the collection from its handler. But it disallows modifying the collection from the CollectionChanged
handler if there are two or more subscribers to the CollectionChanged
event.
The pair of methods BlockReentrancy
and CheckReentancy
are used to implement this logic. The BlockReentrancy
is used at the start of the OnCollectionChanged
method and CheckReentancy
is used in all methods that modify the collection.
This is implementation of BlockReentrancy()
protected IDisposable BlockReentrancy()
{
this._monitor.Enter();
return this._monitor;
}
There is another method CheckReentrancy()
protected void CheckReentrancy()
{
if ((this._monitor.Busy && (this.CollectionChanged != null)) && (this.CollectionChanged.GetInvocationList().Length > 1))
{
throw new InvalidOperationException(SR.GetString("ObservableCollectionReentrancyNotAllowed"));
}
}
Such methods as ClearItems
, InsertItem
, MoveItem
, RemoveItem
, SetItem
check CheckReentrancy()
before modifying collection.
So the code below guarantees that collection will not be changed inside of using
, but only if there is more than one handler subscribed to CollectionChanged
event.
using BlockReentrancy())
{
CollectionChanged(this, e);
}
This example demonstrates effect of BlockReentrancy()
private static void Main()
{
collection.CollectionChanged += CollectionCollectionChanged1;
collection.CollectionChanged += CollectionCollectionChanged2;
collection.Add(1);
}
private static void CollectionCollectionChanged1(object sender, NotifyCollectionChangedEventArgs e)
{
collection.Add(2); // this line will throw exception
}
private static void CollectionCollectionChanged2(object sender, NotifyCollectionChangedEventArgs e)
{
}
Reentrancy is when a method does something directly or indirectly that causes that method to be invoked again, possibly recursively. In this case, the using block should be used inside the OnCollectionChanged delegate if you want to prevent the changing of the collection from within the handler; attempts to change it will throw an exception. If you didn't use it, then any attempts to modify the collection would cause OnCollectionChanged to be called again.