// This is the method that the MailManager will call // when a new e-mail message arrives private void SendMsgToPager(Object sender, NewMailEventArgs e) { // 'sender' identifies the MailManager in case we want to communicate back to it. // 'e' identifies the additional event information that the MailManager wants to give us. // Normally, the code here would send the e-mail message to a pager. // This test implementation displays the info on the console Console.WriteLine("Sending mail message to pager:"); Console.WriteLine(" From={0}, To={1}, Subject={2}", e.From, e.To, e.Subject); }
// Step #4: Define a method that translates the // input into the desired event public void SimulateNewMail(String from, String to, String subject) { // Construct an object to hold the information we wish // to pass to the receivers of our notification NewMailEventArgs e = new NewMailEventArgs(from, to, subject); // Call our virtual method notifying our object that the event // occurred. If no type overrides this method, our object will // notify all the objects that registered interest in the event OnNewMail(e); }
// This is the method the MailManager will call // when a new e-mail message arrives private void FaxMsg(Object sender, NewMailEventArgs e) { // 'sender' identifies the MailManager object in case // we want to communicate back to it. // 'e' identifies the additional event information // the MailManager wants to give us. // Normally, the code here would fax the e-mail message. // This test implementation displays the info in the console Console.WriteLine("Faxing mail message:"); Console.WriteLine(" From={0}, To={1}, Subject={2}", e.From, e.To, e.Subject); }
// Step #3: Define a method responsible for raising the event // to notify registered objects that the event has occurred // If this class is sealed, make this method private and nonvirtual protected virtual void OnNewMail(NewMailEventArgs e) { // Copy a reference to the delegate field now into a temporary field for thread safety //e.Raise(this, ref m_NewMail); #if CompilerImplementedEventMethods EventHandler <NewMailEventArgs> temp = Volatile.Read(ref NewMail); #else EventHandler <NewMailEventArgs> temp = Volatile.Read(ref m_NewMail); #endif // If any methods registered interest with our event, notify them if (temp != null) { temp(this, e); } }