BeginInvoke's BeginInvoke is async, which means it is being created in another thread. This can be dangerous if people do not expect this, and it is quite rare for events, but it can be useful.
Also, note that, strictly speaking, you must take a snapshot of the value of the event handler - this is especially true if (through Begin* ) you are dealing with streams.
var tmp = _myEventHandler; if(tmp != null) { tmp(sender, args); }
Also - note that the event subscription itself is not thread safe; again, this only matters if you are dealing with multi-threaded, but embedded field-like event safe for threads:
public event EventHandler<MyEventArgs> MyEvent;
Here you can avoid problems:
- with the snapshot, we avoid the risk of the subscriber refusing the zero check and call (this means that they can receive an event that they did not expect, but that means we are not killing the upstream)
- by changing the type of field we avoid the risk of losing / unsubscribing when two threads do this at the same time
Marc gravell
source share