ITeye博客

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

zbw · 2007-03-19 13:15 · NET · Markdown

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

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

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。