事件处理程序是否按照它们附加到事件的顺序触发?如果不是,我可以对事件处理程序强制执行某种顺序,以便按特定顺序调用它们吗?
最佳答案
假设事件的简单实现(在委托(delegate)字段上使用 += 和 -= ,而委托(delegate)字段又将使用 Delegate.Combine/Remove
)然后是的,事件处理程序将按照它们被订阅的顺序被调用。 Delegate.Combine
中有效地提供了保证。文档:
Return value
A new multicast (combinable) delegate with an invocation list that concatenates the invocation lists of a and b in that order.
见我的article about events对于其中的一些示例
Delegate.Combine/Remove
做(以及幕后发生的事件)。
关于.net - 如何控制触发事件处理程序的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/264196/