引发和使用事件(引用自MSDN)
下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。
该示例程序使用事件和委托和引发事件中详细说明的概念。
- // 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
引发和使用事件(引用自MSDN)相关推荐
- 一个元素位于另一个元素之上,点击上面的元素引发下面元素事件操作
一个元素位于另一个元素之上,点击上面的元素引发下面元素事件操作 <body><!-- 此布局为: 上面内容盒子覆盖在了上传文本区域之上--><!-- 想要点击'上面盒子内 ...
- .NET 6 “目标进程已退出,但未引发 CoreCLR 启动事件。请确保将目标进程配置为使用 .NET Core。如果目标进程未运行 .NET Core,则发生这种情况并不意外。”
Mac M1 在 .NET 6 上调试.NET 5 的Web应用程序出现程序闪退问题 首先看了下本地 .NET 的环境 ➜ ~ dotnet --list-sdks 6.0.200 [/usr/loc ...
- 目标进程已退出,但未引发 CoreCLR 启动事件
目标进程已退出,但未引发 CoreCLR 启动事件.请确保将目标进程配置为使用 .NET Core.如果目标进程未运行 .NET Core,则发生这种情况并不意外. 程序"[18088] W ...
- sdk缺失”目标进程已退出,但未引发 CoreCLR 启动事件。请确保将目标进程配置为使用 .NET Core。如果目标进程未运行 .NET Core,则发生这种情况并不意外。 程序“[16780]
问题:项目运行后出现"目标进程已退出,但未引发 CoreCLR 启动事件.请确保将目标进程配置为使用 .NET Core.如果目标进程未运行 .NET Core,则发生这种情况并不意外. 程 ...
- 在派生类中引发基类事件
1. 在创建基类时,若涉及到事件,事件是特殊类型的委托,只可以从声明它们的类中调用,派生类无法直接调用基类中声明的事件,但是在多数情况,会需要允许派生类调用基类事件,这时,可以再包含该事件的基类中创 ...
- Sql Server之旅——终点站 nolock引发的三级事件的一些思考
曾今有件事情让我记忆犹新,那年刚来携程不久,马上就被安排写一个接口,供企鹅公司调用他们员工的差旅信息,然后我就三下五除二的给写好了,上线之后,大概过了一个月...DBA那边报告数据库出现大量锁超时,并 ...
- jquery之ajax——全局事件引用方式以及各个事件(全局/局部)执行顺序
jquery中各个事件执行顺序如下: 1.ajaxStart(全局事件) 2.beforeSend(局部事件) 3.ajaxSend(全局事件) 4.success(局部事件) 5.ajaxSucce ...
- 由event target引发的关于事件流的一连串思考(二)
阻止事件冒泡 W3C的方法是ev.stopPropagation(),IE则是使用ev.cancelBubble = true. 先不谈IE的私有方法,首先讨论一个问题:ev.stopPropagat ...
- EventSource 引发的一系列事件
背景 大家好,我是江辰,最近小小的实现了下 chatGPT 的问答式回复,调研了前端如何实现这种问答式请求,有几种方案,Http.EventSource.WebSocket,三种实现方案各有优缺点,H ...
最新文章
- python gui选择_Python之GUI的最终选择(Tkinter)
- 中芯国际再曝内讧,联席 CEO 梁孟松愤然辞职
- 介绍html CSS和JS的定义或引用
- Fedora配置网络DHCP
- 通过自动化机器学习对抗Java恶意软件
- python查找输出文字_Python基础练习,查询文本内容并输出;
- 使用WireMock进行更好的集成测试
- maven scope使用和理解
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- matlab矩阵除法
- Qt开源工业软件收录
- android9应用icon尺寸,APP-icon尺寸
- Geforce GTX 1660Ti + Ubuntu18.04 LTS + Nvidia显卡驱动 +CUDA10 配置安装
- DragonBones快速入门指南1
- 北京中国科学院计算机专业怎么样,中国科学院北京计算技术研究所计算机技术怎么样...
- layui列表筛选列_基于layui实现高级搜索(筛选)功能
- Android Studio kotlin编程实现图片滑动浏览 stepbystep
- P3793礼物和糖果
- UI5-文档-4.5-Controllers
- 最近电平接近 NLM 模块化多电平变换器matlab/simulink仿真模型