Information Technology Reference
In-Depth Information
get { return theOnly; }
}
// Define the event:
public event EventHandler < LoggerEventArgs > Log;
// add a message, and log it.
public void AddMsg( int priority, string msg)
{
// This idiom discussed below.
EventHandler < LoggerEventArgs > l = Log;
if (l != null )
l( this , new LoggerEventArgs (priority, msg));
}
}
The AddMsg method shows the proper way to raise events. The temporary
variable to reference the log event handler is an important safeguard
against race conditions in multithreaded programs. Without the copy of
the reference, clients could remove event handlers between the if state-
ment check and the execution of the event handler. By copying the refer-
ence, that can't happen.
I've defined LoggerEventArgs to hold the priority of an event and the mes-
sage. The delegate defines the signature for the event handler. Inside the
Logger class, the event field defines the event handler. The compiler sees
the public event field definition and creates the Add and Remove opera-
tors for you. The generated code is exactly the same as though you had
written the following:
public class Logger
{
private EventHandler < LoggerEventArgs > log;
public event EventHandler < LoggerEventArgs > Log
{
add { log = log + value ; }
remove { log = log - value ; }
}
 
Search WWH ::




Custom Search