引发和使用事件

下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUpAlarmRang 方法设置为处理 AlarmClockAlarm 事件。


namespace EventSample

   using System;
   using System.ComponentModel;
   
  
   public class AlarmEventArgs : EventArgs
   { 
      private readonly bool snoozePressed ;
      private readonly int nrings;
      
    
      public AlarmEventArgs(bool snoozePressed, int nrings)
      {
         this.snoozePressed = snoozePressed;
         this.nrings = nrings;
      }
      
     
      public int NumRings
      {    
         get { return nrings;}     
      }
      
  
      public bool SnoozePressed
      {
         get {return snoozePressed;}
      }
     
      // The AlarmText property that contains the wake-up message.
      //
      public string AlarmText
      {
         get
         {
            if (snoozePressed)
            {
               return ("Wake Up!!! Snooze time is over.");
            }
            else
            {
               return ("Wake Up!");
            }
         }
      } 
   }
  
   // Delegate declaration.
   //
   public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);  //声明委托类型
   public event AlarmEventHandler Alarm; //用委托类型声明一个变量 该变量可以引用与委托签名相同的所有方法。
   
   // The Alarm class that raises the alarm event.
   //
   public class AlarmClock
   { 
      private bool snoozePressed = false;
      private int nrings = 0;
      private bool stop = false;
     
      // The Stop property indicates whether the
      // alarm should be turned off.
      //
      public bool Stop
      {
         get {return stop;}
         set {stop = value;}
      }
     
      // The SnoozePressed property indicates whether the snooze
      // button is pressed on the alarm when the alarm event is generated.
      //
      public bool SnoozePressed
      {
         get {return snoozePressed;}
         set {snoozePressed = value;}
      }
   
     

      // The protected OnAlarm method raises the event by invoking
      // the delegates. The sender is always this, the current instance
      // of the class.
      //
      protected virtual void OnAlarm(AlarmEventArgs e)
      {
         if (Alarm != null)
         {
            // Invokes the delegates.
            Alarm(this, e);
         }
      }
     
      // This alarm clock does not have
      // a user interface.
      // To simulate the alarm mechanism it has a loop
      // that raises the alarm event at every iteration
      // with a time delay of 300 milliseconds,
      // if snooze is not pressed. If snooze is pressed,
      // the time delay is 1000 milliseconds.
      //
      public void Start()
      {
         for (;;)   
         {
            nrings++;     
            if (stop)
            {
               break;
            }
           
            else if (snoozePressed)
            {
               System.Threading.Thread.Sleep(1000);
               {
                  AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
                     nrings);
                  OnAlarm(e);
               }
            }
            else
            {
               System.Threading.Thread.Sleep(300);
               AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
                  nrings);
               OnAlarm(e);  //引发事件(事件发送方)
            }          
         }
      }
   }
  
   // The WakeMeUp class has a method AlarmRang that handles the
   // alarm event.
   //
   public class WakeMeUp
   {
     
      public void AlarmRang(object sender, AlarmEventArgs e) //事件接收方,事件处理程序
       //事件处理程序必须具有与为事件声明的委托相同的方法签名
      {
        
         Console.WriteLine(e.AlarmText +"\n");
        
         if (!(e.SnoozePressed))
         {
            if (e.NumRings % 10 == 0)
            {
               Console.WriteLine(" Let alarm ring? Enter Y");
               Console.WriteLine(" Press Snooze? Enter N");
               Console.WriteLine(" Stop Alarm? Enter Q");
               String input = Console.ReadLine();
              
               if (input.Equals("Y") ||input.Equals("y")) return;
              
               else if (input.Equals("N") || input.Equals("n"))
               {
                  ((AlarmClock)sender).SnoozePressed = true;
                  return;
               }
               else
               {
                  ((AlarmClock)sender).Stop = true;
                  return;
               }
            }
         }
         else
         {
            Console.WriteLine(" Let alarm ring? Enter Y");
            Console.WriteLine(" Stop Alarm? Enter Q");
            String input = Console.ReadLine();
            if (input.Equals("Y") || input.Equals("y")) return;
            else
            {
               ((AlarmClock)sender).Stop = true;
               return;
            }
         }
      }
   }
  
  
   // The driver class that hooks up the event handling method of
   // WakeMeUp to the alarm event of an Alarm object using a delegate.
   // In a forms-based application, the driver class is the
   // form.
   //
   public class AlarmDriver
   { 
      public static void Main (string[] args)
      { 
         // Instantiates the event receiver.
         WakeMeUp w= new WakeMeUp();
                 
         // Instantiates the event source.
         AlarmClock clock = new AlarmClock();

         // Wires the AlarmRang method to the Alarm event.
         clock.Alarm += new AlarmEventHandler(w.AlarmRang);

         clock.Start();
      }
   }  
}
原文地址:https://www.cnblogs.com/chenghm2003/p/1234639.html