ObservableCollection中的块重入
有人可以向我解释一下 ObservableCollection
中 BlockReentrancy
方法的用途吗?
MSDN 显示以下内容作为示例:
//The typical usage is to wrap an OnCollectionChanged call within a using scope, as in the following example:
using (BlockReentrancy())
{
// OnCollectionChanged call
}
但这似乎并不为我澄清目的是什么。有人愿意解释一下吗?
Could someone please be kind enough to explain to me what the purpose of the BlockReentrancy
Method is in the ObservableCollection<T>
?
MSDN shows the following as an example:
//The typical usage is to wrap an OnCollectionChanged call within a using scope, as in the following example:
using (BlockReentrancy())
{
// OnCollectionChanged call
}
But this doesn't seem to clarify for me what the purpose is. Anyone care to explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ObservableCollection
实现INotifyCollectionChanged
,因此它有一个CollectionChanged
事件。如果有此事件的订阅者,他们可以在集合已处于通知过程中时进一步修改集合。由于CollectionChanged
事件准确跟踪更改的内容,因此这种交互可能会变得非常混乱。因此,作为一种特殊情况,
ObservableCollection
允许CollectionChanged
事件的单个订阅者从其处理程序修改集合。但如果 CollectionChanged 事件有两个或更多订阅者,它不允许从CollectionChanged
处理程序修改集合。BlockReentrancy
和CheckReentancy
这对方法用于实现此逻辑。BlockReentrancy
用于OnCollectionChanged
方法的开头,CheckReentancy
用于修改集合的所有方法。An
ObservableCollection
implementsINotifyCollectionChanged
and so it has aCollectionChanged
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 theCollectionChanged
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 theCollectionChanged
event to modify the collection from its handler. But it disallows modifying the collection from theCollectionChanged
handler if there are two or more subscribers to theCollectionChanged
event.The pair of methods
BlockReentrancy
andCheckReentancy
are used to implement this logic. TheBlockReentrancy
is used at the start of theOnCollectionChanged
method andCheckReentancy
is used in all methods that modify the collection.这是
BlockReentrancy()
的实现还有一个方法
CheckReentrancy()
诸如
ClearItems
、InsertItem
、< code>MoveItem、RemoveItem
、SetItem
在修改集合之前检查CheckReentrancy()
。因此,下面的代码保证集合不会在
using
内部发生更改,但前提是有多个处理程序订阅了CollectionChanged
事件。此示例演示了
BlockReentrancy()
的效果This is implementation of
BlockReentrancy()
There is another method
CheckReentrancy()
Such methods as
ClearItems
,InsertItem
,MoveItem
,RemoveItem
,SetItem
checkCheckReentrancy()
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 toCollectionChanged
event.This example demonstrates effect of
BlockReentrancy()
重入是指一个方法直接或间接执行某些操作,导致该方法被再次调用(可能是递归调用)。在这种情况下,如果您想防止在处理程序中更改集合,则应在 OnCollectionChanged 委托内部使用 using 块;尝试更改它会引发异常。如果您没有使用它,则任何修改集合的尝试都会导致 OnCollectionChanged 再次被调用。
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.
下面是 代码 位于 BlockReentrancy 后面。 CheckReentrancy 在 ObservableCollection 实现中的每个集合修饰符方法开始时调用。
(版权所有 (c) .NET 基金会和贡献者)
Below is the code behind BlockReentrancy. CheckReentrancy is called at the start of each collection modifier method in the implementation of ObservableCollection.
(Copyright (c) .NET Foundation and Contributors)