Are event subscribers called in order of subscription?

前端 未结 5 974
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 16:07

Is it safe to assume that event subscribers are called in order of subscription?
Example:

void One(object sender, EventArgs e) {}
void Two(object sende         


        
5条回答
  •  粉色の甜心
    2020-12-09 16:41

    Pay very close attention to the caveats given by Jon Skeet - "Given that implementation...". In other words, make the slightest change (multiple threads, other handlers, etc.) and you risk losing the order-of-execution invariance.

    Do NOT rely on event ordering. All event dispatches should be logically independent, as if they were occurring in parallel. Events are logically independent actions.

    I'll go one step further, and assert that if you have to assume an order for events firing, you have a serious design flaw and/or are misusing events.

提交回复
热议问题