Question regarding Events in John Skeet's book C# in Depth
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 c开发者_开发知识库ould 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 OnSomeEvent
when it wants to fire off the event.
精彩评论