How can I control the order in which event handlers are fired?

后端 未结 5 1226
庸人自扰
庸人自扰 2021-01-03 23:09

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 specif

5条回答
  •  佛祖请我去吃肉
    2021-01-03 23:55

    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:

    Return value

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

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

提交回复
热议问题