如何控制触发事件处理程序的顺序? [英] How can I control the order in which event handlers are fired?

查看:79
本文介绍了如何控制触发事件处理程序的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件处理程序是否按照附加到事件的顺序启动?如果没有,我可以对事件处理程序执行某种顺序,以便按照特定的顺序进行调用?

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 / Remove )然后是,事件处理程序将按照订阅的顺序调用。该保证在 Delegate.Combine 文档:

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:


返回值

新的多播(可组合)委托
与调用列表
连接 a
b

A new multicast (combinable) delegate with an invocation list that concatenates the invocation lists of a and b in that order.

查看我的关于事件的文章的一些示例,其中 Delegate.Combine / Remove do(以及什么事件在封面下) 。

See my article about events for some examples of which Delegate.Combine/Remove do (and what events are like under the covers).

这篇关于如何控制触发事件处理程序的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆