`
zbw
  • 浏览: 46019 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

引发和使用事件(引用自MSDN)

    博客分类:
  • NET
阅读更多

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

该示例程序使用事件和委托引发事件中详细说明的概念。

c# 代码
  1. // EventSample.cs.   
  2. //   
  3. namespace EventSample   
  4. {     
  5.    using System;   
  6.    using System.ComponentModel;   
  7.       
  8.    // Class that contains the data for    
  9.    // the alarm event. Derives from System.EventArgs.   
  10.    //   
  11.    public class AlarmEventArgs : EventArgs    
  12.    {     
  13.       private readonly bool snoozePressed ;   
  14.       private readonly int nrings;   
  15.          
  16.       //Constructor.   
  17.       //   
  18.       public AlarmEventArgs(bool snoozePressed, int nrings)    
  19.       {   
  20.          this.snoozePressed = snoozePressed;   
  21.          this.nrings = nrings;   
  22.       }   
  23.          
  24.       // The NumRings property returns the number of rings   
  25.       // that the alarm clock has sounded when the alarm event    
  26.       // is generated.   
  27.       //   
  28.       public int NumRings   
  29.       {        
  30.          get { return nrings;}         
  31.       }   
  32.          
  33.       // The SnoozePressed property indicates whether the snooze   
  34.       // button is pressed on the alarm when the alarm event is generated.   
  35.       //   
  36.       public bool SnoozePressed    
  37.       {   
  38.          get {return snoozePressed;}   
  39.       }   
  40.          
  41.       // The AlarmText property that contains the wake-up message.   
  42.       //   
  43.       public string AlarmText    
  44.       {   
  45.          get    
  46.          {   
  47.             if (snoozePressed)   
  48.             {   
  49.                return ("Wake Up!!! Snooze time is over.");   
  50.             }   
  51.             else    
  52.             {   
  53.                return ("Wake Up!");   
  54.             }   
  55.          }   
  56.       }     
  57.    }   
  58.       
  59.    // Delegate declaration.   
  60.    //   
  61.    public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);   
  62.       
  63.    // The Alarm class that raises the alarm event.   
  64.    //   
  65.    public class AlarmClock    
  66.    {     
  67.       private bool snoozePressed = false;   
  68.       private int nrings = 0;   
  69.       private bool stop = false;   
  70.          
  71.       // The Stop property indicates whether the    
  72.       // alarm should be turned off.   
  73.       //   
  74.       public bool Stop    
  75.       {   
  76.          get {return stop;}   
  77.          set {stop = value;}   
  78.       }   
  79.          
  80.       // The SnoozePressed property indicates whether the snooze   
  81.       // button is pressed on the alarm when the alarm event is generated.   
  82.       //   
  83.       public bool SnoozePressed   
  84.       {   
  85.          get {return snoozePressed;}   
  86.          set {snoozePressed = value;}   
  87.       }   
  88.       // The event member that is of type AlarmEventHandler.   
  89.       //   
  90.       public event AlarmEventHandler Alarm;   
  91.   
  92.       // The protected OnAlarm method raises the event by invoking    
  93.       // the delegates. The sender is always this, the current instance    
  94.       // of the class.   
  95.       //   
  96.       protected virtual void OnAlarm(AlarmEventArgs e)   
  97.       {   
  98.         AlarmEventHandler handler = Alarm;    
  99.         if (handler != null)    
  100.         {    
  101.            // Invokes the delegates.    
  102.            handler(this, e);    
  103.         }   
  104.       }   
  105.          
  106.       // This alarm clock does not have   
  107.       // a user interface.    
  108.       // To simulate the alarm mechanism it has a loop   
  109.       // that raises the alarm event at every iteration   
  110.       // with a time delay of 300 milliseconds,   
  111.       // if snooze is not pressed. If snooze is pressed,   
  112.       // the time delay is 1000 milliseconds.   
  113.       //   
  114.       public void Start()   
  115.       {   
  116.          for (;;)       
  117.          {   
  118.             nrings++;         
  119.             if (stop)   
  120.             {   
  121.                break;   
  122.             }   
  123.                
  124.             else if (snoozePressed)   
  125.             {   
  126.                System.Threading.Thread.Sleep(1000);   
  127.                {   
  128.                   AlarmEventArgs e = new AlarmEventArgs(snoozePressed,    
  129.                      nrings);   
  130.                   OnAlarm(e);   
  131.                }   
  132.             }   
  133.             else  
  134.             {   
  135.                System.Threading.Thread.Sleep(300);   
  136.                AlarmEventArgs e = new AlarmEventArgs(snoozePressed,    
  137.                   nrings);   
  138.                OnAlarm(e);   
  139.             }              
  140.          }   
  141.       }   
  142.    }   
  143.       
  144.    // The WakeMeUp class has a method AlarmRang that handles the   
  145.    // alarm event.   
  146.    //   
  147.    public class WakeMeUp   
  148.    {   
  149.          
  150.       public void AlarmRang(object sender, AlarmEventArgs e)   
  151.       {   
  152.             
  153.          Console.WriteLine(e.AlarmText +"\n");   
  154.             
  155.          if (!(e.SnoozePressed))   
  156.          {   
  157.             if (e.NumRings % 10 == 0)   
  158.             {   
  159.                Console.WriteLine(" Let alarm ring? Enter Y");   
  160.                Console.WriteLine(" Press Snooze? Enter N");    
  161.                Console.WriteLine(" Stop Alarm? Enter Q");   
  162.                String input = Console.ReadLine();   
  163.                   
  164.                if (input.Equals("Y") ||input.Equals("y")) return;   
  165.                   
  166.                else if (input.Equals("N") || input.Equals("n"))   
  167.                {   
  168.                   ((AlarmClock)sender).SnoozePressed = true;   
  169.                   return;   
  170.                }   
  171.                else  
  172.                {   
  173.                   ((AlarmClock)sender).Stop = true;   
  174.                   return;   
  175.                }   
  176.             }   
  177.          }   
  178.          else  
  179.          {   
  180.             Console.WriteLine(" Let alarm ring? Enter Y");    
  181.             Console.WriteLine(" Stop Alarm? Enter Q");   
  182.             String input = Console.ReadLine();   
  183.             if (input.Equals("Y") || input.Equals("y")) return;   
  184.             else    
  185.             {   
  186.                ((AlarmClock)sender).Stop = true;   
  187.                return;   
  188.             }   
  189.          }   
  190.       }   
  191.    }   
  192.       
  193.       
  194.    // The driver class that hooks up the event handling method of   
  195.    // WakeMeUp to the alarm event of an Alarm object using a delegate.   
  196.    // In a forms-based application, the driver class is the   
  197.    // form.   
  198.    //   
  199.    public class AlarmDriver   
  200.    {     
  201.       public static void Main (string[] args)   
  202.       {     
  203.          // Instantiates the event receiver.   
  204.          WakeMeUp w= new WakeMeUp();   
  205.                      
  206.          // Instantiates the event source.   
  207.          AlarmClock clock = new AlarmClock();   
  208.   
  209.          // Wires the AlarmRang method to the Alarm event.   
  210.          clock.Alarm += new AlarmEventHandler(w.AlarmRang);   
  211.   
  212.          clock.Start();   
  213.       }   
  214.    }      
  215. }  

地址:http://msdn2.microsoft.com/zh-cn/library/9aackb16(VS.80).aspx

分享到:
评论

相关推荐

    MSDN2005绿色便携版MSDN

    首先声明,这份资源是amo_xoo的毛哥.NET 2.0便携参考(绿色MSDN) 2.0 彻底版,但他那个分了9个包,下载起来很不方便,所以就引用过来,方便大家下载。 微软MSDN在线库虽然全,但有时不方便上网,而MSDN Library安装...

    MSDN绿色U盘版-14

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN帮助系统的使用

    何如快捷地使用VS.net的MSDN帮助系统

    MSDN帮助文档中文.zip

    最新MSDN帮助文档中文版 离线包,自己存储使用 最新MSDN帮助文档中文版 离线包,自己存储使用 最新MSDN帮助文档中文版 离线包,自己存储使用 小白方便查看

    VS Code MSDN手册 01.安装和使用 摘译.pdf

    VS Code MSDN手册的翻译稿。这是第一部分----安装和使用。后续部分正在翻译中。 有注释,带书签,主要针对:Windows环境/通用部分/Python部分进行了翻译。 该文件“……01.……”是其中的第一部分

    MSDN绿色U盘版-15

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN绿色U盘版-8

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN绿色U盘版-3

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN绿色U盘版-16

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN绿色U盘版-9

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN绿色U盘版-5

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN绿色U盘版-1(共17包)

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    MSDN2005绿色便携版

    首先声明,这份资源是amo_xoo的毛哥.NET 2.0便携参考(绿色MSDN) 2.0 彻底版,但他那个分了9个包,下载起来很不方便,所以就引用过来,方便大家下载。 微软MSDN在线库虽然全,但有时不方便上网,而MSDN Library安装...

    MSDN-library-vchelp.rar_MSDN library chm_MSDN-library_msdn_msdn

    vc++6.0 MSDN Library chm

    MSDN绿色U盘版-17(完,共17包)

    本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...

    C# 文档msdn.pdf

    C#msdn文档,内容非常齐全,堪称C#词典 pdf格式带有书签,可作为C#工具书使用,检索学习方便 本人做C#开发5年常翻阅,各位可根据情况下载使用

    MSDN离线库更新至20201102

    2. 在VS界面中,“帮助”-“添加和删除帮助内容”,启动Help Viewer 2.3 3. 在“管理内容”页面中,找到“本地存储路径”(也可修改路径到指定目录) 4. 将自制的MSDN离线文件ISO解压到上述路径,覆盖原来的文件。 5...

    [MSDN].msdn_oct_2001

    [MSDN].msdn_oct_2001 适用于vc++6.0,最后一个版本。分成7个文件上传

    VC++ 6.0 msdn中文版

    中文版msdn,需然小了点,不过对入门都来说是不错的选择....

    C#基础回顾:C#语言规范[本部分转自MSDN] .doc

    C#基础回顾:C#语言规范[本部分转自MSDN] .doc

Global site tag (gtag.js) - Google Analytics