TypeName | CopyEventToVariableBeforeFireAnalyzer |
Check Id | CC0016 |
Category | Design |
Severity | Warning |
Events should always be checked for null before being invoked.
As in a multi-threading context it is possible for an event to be unsuscribed between the moment where it is checked to be non-null and the moment it is raised, the event must be copied to a temporary variable before the check.
public event System.EventHandler MyEvent;
public void Execute()
{
MyEvent(this, System.EventArgs.Empty);
}
A code fix will be presented to you that will transform the code:
public event System.EventHandler MyEvent;
public void Execute()
{
var handler = MyEvent;
if (handler != null)
handler(this, System.EventArgs.Empty);
}
TBD