Esta es la aplicación de BlockReentrancy()
protected IDisposable BlockReentrancy()
{
this._monitor.Enter();
return this._monitor;
}
Hay otro método CheckReentrancy()
protected void CheckReentrancy()
{
if ((this._monitor.Busy && (this.CollectionChanged != null)) && (this.CollectionChanged.GetInvocationList().Length > 1))
{
throw new InvalidOperationException(SR.GetString("ObservableCollectionReentrancyNotAllowed"));
}
}
Tales métodos como ClearItems
, InsertItem
, MoveItem
, RemoveItem
, SetItem
cheque CheckReentrancy()
antes de modificar colección.
Por lo tanto, el siguiente código garantiza que la recopilación no se cambiará dentro de using
, pero solo si hay más de un controlador suscrito al evento CollectionChanged
.
using BlockReentrancy())
{
CollectionChanged(this, e);
}
Este ejemplo demuestra el efecto de 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)
{
}
Pronunciarlo 'BlockReëntrancy' –