Event handlers not thread safe? [duplicate]

IMO, the other answers miss one key detail – that delegates (and therefore events) are immutable. The significance of this is that subscribing or unsubscribing an event handler doesn’t simply append/remove to a list – rather, it replaces the list with a new one with an extra (or one less) item on it.

Since references are atomic, this means that at the point you do:

var handler = SomeEvent;

you now have a rigid instance that cannot change, even if in the next picosecond another thread unsubscribes (causing the actual event field to become null).

So you test for null and invoke it, and all is well. Note of course that there is still the confusing scenario of the event being raised on an object that thinks it unsubscribed a picosecond ago!

Leave a Comment