Use delegate mechanismIf you want to use message queue, refer to System.Message
If you want to use message in web service, refer to System.Web.Services.Description
If you want to use message in web service, refer to System.Web.Services.Description
解决方案 »
- 泛型元素,如何组成字符串呢?
- 关于分类多选,如何查询某个分类的信息,求SQL句子
- C# 中如何去除指定的字符
- vs2005连接SQL2005中连接字符串的灵异事件!!!
- 最基础的,为什么在类A中定义了变量B,C在其他页面(**.ASPX)的代码叶不能引用
- 如何知道select 语后的行数?如何将select的某一列绑定到一个ArrayList中?
- 用C#在winform中做一个生成关系图的功能
- 交替字符串
- 将数据从datagridview中导出到excel中,导出完毕后自动保存到给定的路径中。该怎么处理
- 继昨天那帖子需求帮助,谢谢了。
- 寻找SelectCommand.Connection 属性尚未初始化错误解决办法?
- 怎么把DataSet中的Table的数据导入SQL中?急
Handling and Raising Events
Language
C#Visual BasicShow All
The sample presented here builds on the various pieces discussed in Events and Delegates and Raising an Event.This sample shows how to raise an event from your class and also how to handle the event. It defines the following classes. AlarmClock is the class that raises the Alarm event.
AlarmEventArgs defines the data for the Alarm event.
AlarmEventHandler is the delegate for the alarm event.
WakeMeUp is a class that has a method, AlarmRang, that handles the alarm event.
AlarmDriver is a class that demonstrates how events are wired. It instantiates AlarmClock and WakeMeUp. It then instantiates the AlarmEventHandler delegate with a reference to the AlarmRang method of the WakeMeUp instance. AlarmDriver completes the event wiring by registering the delegate with the instance of AlarmClock, using the += syntax for adding delegates to an event.
To compile and run this sample Copy the code below to a source file (such as EventSample.cs.or EventSample.vb).
From the directory that contains source file, execute the following command:
[C#]
csc /r:System.dll EventSample.cs
[Visual Basic]
vbc /r:System.dll EventSample.vb(The /r option references the System assembly.) Run the executable that is created, EventSample.exe.
[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!");
}
}
}
}
//
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)
{
if (Alarm != null)
{
// Invokes the delegates.
Alarm(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 that 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();
}
}
}