G
Guest
This questions is in reference to the article on Using Events in the C#
Programmers Reference
(http://msdn2.microsoft.com/en-us/library/ms173168.aspx).
Under the Rasing Events section it says in order to avoid a race condition
copy the event to a temporary variable before invoking it. That makes sense.
My question is is this: if we really want this to be thread safe why doesn't
this copy have to be within a locked block. For example, wouldn't this be a
better solution:
lock (this)
{
// Safely invoke an event:
TestEventDelegate temp = TestEvent;
}
if (temp != null)
{
temp(this, new System.EventArgs());
}
Or is there something I'm missing here?
Thanks,
Programmers Reference
(http://msdn2.microsoft.com/en-us/library/ms173168.aspx).
Under the Rasing Events section it says in order to avoid a race condition
copy the event to a temporary variable before invoking it. That makes sense.
My question is is this: if we really want this to be thread safe why doesn't
this copy have to be within a locked block. For example, wouldn't this be a
better solution:
lock (this)
{
// Safely invoke an event:
TestEventDelegate temp = TestEvent;
}
if (temp != null)
{
temp(this, new System.EventArgs());
}
Or is there something I'm missing here?
Thanks,