// Pass the MailManager object to the constructor public Pager(MailManager mm) { // Construct an instance of the ProcessMailMsgEventHandler // delegate that refers to our SendMsgToPager callback method. // Register our callback with MailManager's ProcessMailMsg event mm.NewMail += SendMsgToPager; }
// Pass the MailManager object to the constructor public Fax(MailManager mm) { // Construct an instance of the EventHandler<NewMailEventArgs> // delegate that refers to our FaxMsg callback method. // Register our callback with MailManager's NewMail event mm.NewMail += FaxMsg; }
public static void Go() { // Construct a MailManager object MailManager mm = new MailManager(); // Construct a Fax object passing it the MailManager object Fax fax = new Fax(mm); // Construct a Pager object passing it the MailManager object Pager pager = new Pager(mm); // Simulate an incoming mail message mm.SimulateNewMail("Jeffrey", "Kristin", "I Love You!"); // Force the Fax object to unregister itself with the MailManager fax.Unregister(mm); // Simulate an incoming mail message mm.SimulateNewMail("Jeffrey", "Mom & Dad", "Happy Birthday."); }
static void Main(string[] args) { MailManager.Go(); TypeWithLotsOfEventsTest(); }
public void Unregister(MailManager mm) { // Unregister ourself with MailManager's ProcessMailMsg event mm.NewMail -= SendMsgToPager; }
// This method could be executed to have the Fax object unregister itself with the NewMail // event so that it no longer receives notifications public void Unregister(MailManager mm) { // Unregister ourself with MailManager's NewMail event mm.NewMail -= FaxMsg; }