यह एक और विधि के रूप में ClearItems
, InsertItem
, MoveItem
, RemoveItem
, SetItem
संग्रह संशोधित करने से पहले जांच CheckReentrancy()
CheckReentrancy()
protected void CheckReentrancy()
{
if ((this._monitor.Busy && (this.CollectionChanged != null)) && (this.CollectionChanged.GetInvocationList().Length > 1))
{
throw new InvalidOperationException(SR.GetString("ObservableCollectionReentrancyNotAllowed"));
}
}
इस तरह के तरीके है BlockReentrancy()
protected IDisposable BlockReentrancy()
{
this._monitor.Enter();
return this._monitor;
}
का कार्यान्वयन है।
तो नीचे दिया गया कोड गारंटी देता है कि संग्रह using
के अंदर नहीं बदला जाएगा, लेकिन केवल तभी होगा जब एक से अधिक हैंडलर CollectionChanged
ईवेंट की सदस्यता लें।
using BlockReentrancy())
{
CollectionChanged(this, e);
}
यह उदाहरण के 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)
{
}
स्रोत
2011-06-06 03:07:26
इसे 'BlockReëntrancy' –