---
title: "引发和使用事件(引用自MSDN)"
author: "zbw"
date: "2007-03-19T13:15:00Z"
tags: []
slug: "msdn"
draft: false
lastmod: "2007-03-19T13:15:00Z"
id: 60893
url: "https://zzz.iteye.com/blog/60893"
---

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

该示例程序使用[事件和委托](http://msdn2.microsoft.com/zh-cn/library/17sde2xt%28VS.80%29.aspx)和[引发事件](http://msdn2.microsoft.com/zh-cn/library/wkzf914z%28VS.80%29.aspx)中详细说明的概念。

c# 代码

001. // EventSample.cs.
002. //
003. namespace EventSample
004. {
005. using System;
006. using System.ComponentModel;
007. // Class that contains the data for
008. // the alarm event. Derives from System.EventArgs.
009. //
010. public class AlarmEventArgs : EventArgs
011. {
012. private readonly bool snoozePressed ;
013. private readonly int nrings;
014. //Constructor.
015. //
016. public AlarmEventArgs(bool snoozePressed, int nrings)
017. {
018. this.snoozePressed = snoozePressed;
019. this.nrings = nrings;
020. }
021. // The NumRings property returns the number of rings
022. // that the alarm clock has sounded when the alarm event
023. // is generated.
024. //
025. public int NumRings
026. {
027. get { return nrings;}
028. }
029. // The SnoozePressed property indicates whether the snooze
030. // button is pressed on the alarm when the alarm event is generated.
031. //
032. public bool SnoozePressed
033. {
034. get {return snoozePressed;}
035. }
036. // The AlarmText property that contains the wake-up message.
037. //
038. public string AlarmText
039. {
040. get
041. {
042. if (snoozePressed)
043. {
044. return ("Wake Up!!! Snooze time is over.");
045. }
046. else
047. {
048. return ("Wake Up!");
049. }
050. }
051. }
052. }
053. // Delegate declaration.
054. //
055. public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
056. // The Alarm class that raises the alarm event.
057. //
058. public class AlarmClock
059. {
060. private bool snoozePressed = false;
061. private int nrings = 0;
062. private bool stop = false;
063. // The Stop property indicates whether the
064. // alarm should be turned off.
065. //
066. public bool Stop
067. {
068. get {return stop;}
069. set {stop = value;}
070. }
071. // The SnoozePressed property indicates whether the snooze
072. // button is pressed on the alarm when the alarm event is generated.
073. //
074. public bool SnoozePressed
075. {
076. get {return snoozePressed;}
077. set {snoozePressed = value;}
078. }
079. // The event member that is of type AlarmEventHandler.
080. //
081. public event AlarmEventHandler Alarm;
082. // The protected OnAlarm method raises the event by invoking
083. // the delegates. The sender is always this, the current instance
084. // of the class.
085. //
086. protected virtual void OnAlarm(AlarmEventArgs e)
087. {
088. AlarmEventHandler handler = Alarm;
089. if (handler != null)
090. {
091. // Invokes the delegates.
092. handler(this, e);
093. }
094. }
095. // This alarm clock does not have
096. // a user interface.
097. // To simulate the alarm mechanism it has a loop
098. // that raises the alarm event at every iteration
099. // 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