下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。
c# 代码
- // EventSample.cs.
- //
- namespace EventSample
- {
- using System;
- using System.ComponentModel;
- // Class that contains the data for
- // the alarm event. Derives from System.EventArgs.
- //
- public class AlarmEventArgs : EventArgs
- {
- private readonly bool snoozePressed ;
- private readonly int nrings;
- //Constructor.
- //
- public AlarmEventArgs(bool snoozePressed, int nrings)
- {
- this.snoozePressed = snoozePressed;
- this.nrings = nrings;
- }
- // The NumRings property returns the number of rings
- // that the alarm clock has sounded when the alarm event
- // is generated.
- //
- public int NumRings
- {
- get { return nrings;}
- }
- // 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;}
- }
- // 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);
- // 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 event member that is of type AlarmEventHandler.
- //
- public event AlarmEventHandler Alarm;
- // 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)
- {
- AlarmEventHandler handler = Alarm;
- if (handler != null)
- {
- // Invokes the delegates.
- handler(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();
- }
- }
- }
地址:http://msdn2.microsoft.com/zh-cn/library/9aackb16(VS.80).aspx
相关推荐
首先声明,这份资源是amo_xoo的毛哥.NET 2.0便携参考(绿色MSDN) 2.0 彻底版,但他那个分了9个包,下载起来很不方便,所以就引用过来,方便大家下载。 微软MSDN在线库虽然全,但有时不方便上网,而MSDN Library安装...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
何如快捷地使用VS.net的MSDN帮助系统
最新MSDN帮助文档中文版 离线包,自己存储使用 最新MSDN帮助文档中文版 离线包,自己存储使用 最新MSDN帮助文档中文版 离线包,自己存储使用 小白方便查看
VS Code MSDN手册的翻译稿。这是第一部分----安装和使用。后续部分正在翻译中。 有注释,带书签,主要针对:Windows环境/通用部分/Python部分进行了翻译。 该文件“……01.……”是其中的第一部分
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
首先声明,这份资源是amo_xoo的毛哥.NET 2.0便携参考(绿色MSDN) 2.0 彻底版,但他那个分了9个包,下载起来很不方便,所以就引用过来,方便大家下载。 微软MSDN在线库虽然全,但有时不方便上网,而MSDN Library安装...
vc++6.0 MSDN Library chm
本绿色版包含了Windows SDK(MSDN 2001中的,含DirectX9),Windows DDK (2003 DDK版本),Visual C++(MFC和VC编译器等工具参考)。应该说包含了VC++编程所需要的所有MSDN内容。本版解压197M,可以放在U盘中运行。...
C#msdn文档,内容非常齐全,堪称C#词典 pdf格式带有书签,可作为C#工具书使用,检索学习方便 本人做C#开发5年常翻阅,各位可根据情况下载使用
2. 在VS界面中,“帮助”-“添加和删除帮助内容”,启动Help Viewer 2.3 3. 在“管理内容”页面中,找到“本地存储路径”(也可修改路径到指定目录) 4. 将自制的MSDN离线文件ISO解压到上述路径,覆盖原来的文件。 5...
[MSDN].msdn_oct_2001 适用于vc++6.0,最后一个版本。分成7个文件上传
中文版msdn,需然小了点,不过对入门都来说是不错的选择....
C#基础回顾:C#语言规范[本部分转自MSDN] .doc