I’m studying C# Events on this link and am a little lost on when the following code is called in the context of Main()
/// <summary>
/// Raises the SomeEvent event
/// </summary>
protected virtual OnSomeEvent(EventArgs e)
{
SomeEventHandler handler;
lock (someEventLock)
{
handler = someEvent;
}
if (handler != null)
{
handler (this, e);
}
}
It’s code that is right above the sentence
“You could use a single lock for all your events”
Question:
How or when does “OnSomeEvent” get called? I’m not asking about variable locking (as-is the context of the code sample) rather, I’m asking when does the protected virtual method pasted above get called?
The class calls
OnSomeEventwhen it wants to fire off the event.