我猜测C#的事件处理程序具有一个监听器列表,当发送消息时会循环遍历该列表。我的问题是它在内部是如何工作的。在循环遍历之前,它是否复制了列表?如果是这样的话,如果有人在列表被复制后但尚未收到消息时注销自己,会发生什么?
即使已注销,它仍然会收到消息吗?
即使已注销,它仍然会收到消息吗?
var handler = SomeEvent;
// <===== another thread could unsubscribe at this point
if(handler != null) handler(sender, args); // <== or part way through this invoke
// (and it either case, have the event trigger even though they think they have
// unsubscribed)
因此,如果你正在编写与事件相关的复杂多线程代码,你应该采用防御性编码方法,以便在你认为已经取消订阅后触发的事件不会成为问题。
这些细微差别并不会真正影响单线程代码。