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

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

引发和使用事件(引用自MSDN)相关推荐

  1. 一个元素位于另一个元素之上,点击上面的元素引发下面元素事件操作

    一个元素位于另一个元素之上,点击上面的元素引发下面元素事件操作 <body><!-- 此布局为: 上面内容盒子覆盖在了上传文本区域之上--><!-- 想要点击'上面盒子内 ...

  2. .NET 6 “目标进程已退出,但未引发 CoreCLR 启动事件。请确保将目标进程配置为使用 .NET Core。如果目标进程未运行 .NET Core,则发生这种情况并不意外。”

    Mac M1 在 .NET 6 上调试.NET 5 的Web应用程序出现程序闪退问题 首先看了下本地 .NET 的环境 ➜ ~ dotnet --list-sdks 6.0.200 [/usr/loc ...

  3. 目标进程已退出,但未引发 CoreCLR 启动事件

    目标进程已退出,但未引发 CoreCLR 启动事件.请确保将目标进程配置为使用 .NET Core.如果目标进程未运行 .NET Core,则发生这种情况并不意外. 程序"[18088] W ...

  4. sdk缺失”目标进程已退出,但未引发 CoreCLR 启动事件。请确保将目标进程配置为使用 .NET Core。如果目标进程未运行 .NET Core,则发生这种情况并不意外。 程序“[16780]

    问题:项目运行后出现"目标进程已退出,但未引发 CoreCLR 启动事件.请确保将目标进程配置为使用 .NET Core.如果目标进程未运行 .NET Core,则发生这种情况并不意外. 程 ...

  5. 在派生类中引发基类事件

    1.  在创建基类时,若涉及到事件,事件是特殊类型的委托,只可以从声明它们的类中调用,派生类无法直接调用基类中声明的事件,但是在多数情况,会需要允许派生类调用基类事件,这时,可以再包含该事件的基类中创 ...

  6. Sql Server之旅——终点站 nolock引发的三级事件的一些思考

    曾今有件事情让我记忆犹新,那年刚来携程不久,马上就被安排写一个接口,供企鹅公司调用他们员工的差旅信息,然后我就三下五除二的给写好了,上线之后,大概过了一个月...DBA那边报告数据库出现大量锁超时,并 ...

  7. jquery之ajax——全局事件引用方式以及各个事件(全局/局部)执行顺序

    jquery中各个事件执行顺序如下: 1.ajaxStart(全局事件) 2.beforeSend(局部事件) 3.ajaxSend(全局事件) 4.success(局部事件) 5.ajaxSucce ...

  8. 由event target引发的关于事件流的一连串思考(二)

    阻止事件冒泡 W3C的方法是ev.stopPropagation(),IE则是使用ev.cancelBubble = true. 先不谈IE的私有方法,首先讨论一个问题:ev.stopPropagat ...

  9. EventSource 引发的一系列事件

    背景 大家好,我是江辰,最近小小的实现了下 chatGPT 的问答式回复,调研了前端如何实现这种问答式请求,有几种方案,Http.EventSource.WebSocket,三种实现方案各有优缺点,H ...

最新文章

  1. python gui选择_Python之GUI的最终选择(Tkinter)
  2. 中芯国际再曝内讧,联席 CEO 梁孟松愤然辞职
  3. 介绍html CSS和JS的定义或引用
  4. Fedora配置网络DHCP
  5. 通过自动化机器学习对抗Java恶意软件
  6. python查找输出文字_Python基础练习,查询文本内容并输出;
  7. 使用WireMock进行更好的集成测试
  8. maven scope使用和理解
  9. python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
  10. matlab矩阵除法
  11. Qt开源工业软件收录
  12. android9应用icon尺寸,APP-icon尺寸
  13. Geforce GTX 1660Ti + Ubuntu18.04 LTS + Nvidia显卡驱动 +CUDA10 配置安装
  14. DragonBones快速入门指南1
  15. 北京中国科学院计算机专业怎么样,中国科学院北京计算技术研究所计算机技术怎么样...
  16. layui列表筛选列_基于layui实现高级搜索(筛选)功能
  17. Android Studio kotlin编程实现图片滑动浏览 stepbystep
  18. P3793礼物和糖果
  19. UI5-文档-4.5-Controllers
  20. 最近电平接近 NLM 模块化多电平变换器matlab/simulink仿真模型

热门文章

  1. 系统规划---方案的制订和改进
  2. 江门中微子项目预计2021年建成 有望首测中微子质量顺序
  3. android 没有地磁, gps如何导航,室内没有GPS信号,要怎么精确导航?
  4. java中Keytool生成证书
  5. Win10系统更新提示错误0xc1900403的解决方法
  6. 量子革命?脑机接口?电子皮肤?我看了一次高质量科学大会
  7. 不明所以然,就被KO了,内部人员道出真相,原因竟在这!
  8. DecoHack #014 独立产品灵感周刊 - 有些产品很无用但又有很有趣
  9. Java学习--day02---运算,一些重要的程序
  10. hive数据备份与恢复