Hi,
Yes, there is a possible race condition here;
if (ItemEnabledChanged !=
null)
ItemEnabledChanged(
this, e);
A thread switch can occur between the 'if' and the ItemEnabledChanged call, and that can lead to a null reference exception if during that thread switch all event handlers are disconnected. You can sort of solve it by doing this;
MyItemEnabledChangedEventHandler eventDelegate = ItemEnabledChanged;
if (eventDelegate != null)
eventDelegate(this, e);
Multi-cast delegates (used by events) are immtuable, so getting a reference to the the delegate and then using that rather than the shared variable guarantees you won't get the null exception because the invocation list can't change for the reference you first got. However, this isn't perfect either.... there is no absolutely safe way of raising an event and getting predictable behaviour in a multi-threaded scenario in C#, at least not without putting additional logic in each event handler as well.
To see the possible problems that can still occur using the above pattern, see Eric Lippert's excellent blog article here;
http://blogs.msdn.com/ericlippert/archive/2009/04/29/events-and-races.aspx