如何控制其中的事件处理程序的顺序? [英] How can I control the order in which event handlers are fired?
问题描述
是事件处理解雇的顺序,它们连接到事件?如果没有,我可以实施某种次序到的事件处理程序,使得它们被称为在一个特定的顺序
Are event handlers fired in the order that they attached to the event? If not, can I enforce some kind of order onto the event handlers such that they are called in a specific order?
推荐答案
假设一个简单实现的情况下(使用+ =和 - =的委托场,这反过来会使用 Delegate.Combine /删除
)然后是,事件处理程序将调用在他们已订阅的顺序。该担保在 Delegate.Combine
<有效送达/ A>文件:
Assuming a simple implementation of the event (using += and -= on a delegate field, which in turn will use Delegate.Combine/Remove
) then yes, the event handlers will be called in the order in which they're subscribed. The guarantee is effectively given in the Delegate.Combine
documentation:
返回值
一个新的组播(可组合)委托 与调用列表 并置的调用列表的在的 和 B 的的顺序。
A new multicast (combinable) delegate with an invocation list that concatenates the invocation lists of a and b in that order.
请参阅我的文章关于事件的一些例子,其中 Delegate.Combine /删除
做的(以及什么事件都喜欢在幕后)。
See my article about events for some examples of which Delegate.Combine/Remove
do (and what events are like under the covers).
这篇关于如何控制其中的事件处理程序的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!