日期:2013-07-07  浏览次数:20543 次

Figure 2.8 Continued
Continued.78 Chapter 2 • Introducing C# Programming
// appears in the message queue. Notice the signature matches
// that requried by AddEventCallback
public void logAddRequest( string FirstName, string LastName,
string MiddleName, string SSN )
{
string name = FirstName + " " + MiddleName + " " + LastName;
FileStream stream = new FileStream( m_fileName,
FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter writer = new StreamWriter( stream );
writer.BaseStream.Seek( 0, SeekOrigin.End );
writer.Write("{0} {1} \n", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
writer.Write( "Adding employee - Name: {0}, SSN: {1}",
name, SSN );
writer.Write("\n------------------------------------\n\n");
writer.Flush();
writer.Close();
}
}
A new class, EmployeeQueueLogger, has been added. It has a method
logAddRequest, which logs requests to add employees to a log file. The important
thing to note is that the logAddRequest method has a signature that matches the
AddEventCallback delegate signature. An instance of the logger is created in the
constructor of EmployeeQueueMonitor. The code that wires up the delegates is also
in the constructor and is shown here:
m_logger = new EmployeeQueueLogger( "log.txt" );
m_addEventCallback = new AddEventCallback( this.addEmployee );
m_addEventCallback += new AddEventCallback(
m_logger.logAddRequest );
http://www.syngress.com
Figure 2.8 Continued.Introducing C# Programming • Chapter 2 79
First, a new logger instance is created. Next, the delegate is initialized with a
first callback function to the addEmployee method of EmployeeQueueMonitor.
Finally, a second callback is added to the delegate, which will invoke the
logAddRequest of the EmployeeQueueLogger class. Notice that the plus sign is used
to add the second callback to the delegate. The plus sign (addition operator) has
been overloaded in the System.Delegate class of the .NET Framework to call the
Combine method of that class.The Combine method adds the callback to the list of
methods the delegate maintains.The minus sign (subtraction operator) is also
overloaded to call the Remove method, which removes a callback from the list of
methods the delegate maintains.The rest of the source code remains unchanged.
When the delegate is invoked in the start method of EmployeeQueueMonitor, both
EmployeeQueueMonitor.addEmployee and EmployeeQueueLogger.logAddRequest are
executed.
Events
The event model is often referred to as the publish/subscribe model or the listener
pattern. The idea behind the event model is that a class publishes the events that it
can raise. Consumers of the class object subscribe to the events they are interested
in.When the event occurs, the object that monitors the event notifies all sub-scribers
that the event has been raised.The subscribers then take some action.
The event model is often used in GUI programs. Handlers are set up for
common events, such as pressing a button.When the button press event occurs,
all subscribers registered for the button press event are invoked.The .NET
Framework uses the event model and in particular the System.Event delegate for
Windows Forms–based applications.
The .NET Framework supplies a built in delegate of type System.Event. The
idea of events in the .NET Framework is to supply a single signature for the del-egate
regardless of the data that is passed to the subscribed callback. One of the
arguments for the Event delegate is an object derived from the .NET Framework
class System.EventArgs, which contains the data the callback needs.You declare a
class derived from System.EventArgs with the data your callback needs.When the
e