当前位置: 移动技术网 > IT编程>开发语言>c# > C#中事件处理的个人体会

C#中事件处理的个人体会

2019年07月18日  | 移动技术网IT编程  | 我要评论
作者: juky_huang 事件的简单解释:

事件是对象发送的消息,以发信号通知操作的发生。操作可能是由用户交互(例如鼠标单击)引起的,也可能是由某些其他的程序逻辑触发的。引发(触发)事件的对象叫做事件发送方。捕获事件并对其作出响应的对象叫做事件接收方。

在事件通信中,事件发送方类不知道哪个对象或方法将接收到(处理)它引发的事件。所需要的是在源和接收方之间存在一个媒介(或类似指针的机制)。.net framework 定义了一个特殊的类型(delegate),该类型提供函数指针的功能。

与其他的类不同,委托类具有一个签名,并且它只能对与其签名匹配的方法进行引用。这样,委托就等效于一个类型安全函数指针或一个回调。

c#中使用事件需要的步骤:

创建一个委托
将创建的委托与特定事件关联(.net类库中的很多事件都是已经定制好的,所以他们也就有相应的一个委托,在编写关联事件处理程序--也就是当有事件发生时我们要执行的方法的时候我们需要和这个委托有相同的签名)
编写事件处理程序
利用编写的事件处理程序生成一个委托实例
把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件
c#中事件产生和实现的流程:

定义a为产生事件的实例,a为a产生的一个事件
定义b为接收事件的实例,b为处理事件的方法
a由于用户(程序编写者或程序使用者)或者系统产生一个a事件(例如点击一个button,产生一个click事件)
a通过事件列表中的委托对象将这个事件通知给b
b接到一个事件通知(实际是b.b利用委托来实现事件的接收)
调用b.b方法完成事件处理
下面给出《c#入门经典》的例子,并做一定的解释:

//====================connection.cs===========
//事件定义,也就是上面提到的a
//============================================
using system;
using system.timers;

namespace ch12ex02
{
/// <summary>
/// connection 的摘要说明。
/// </summary>
/// 

public delegate void messagehandler(string messagetext);//创建一个委托---步骤1
public class connection
{
public event messagehandler messagearrived;//将创建的委托和特定事件关联,在这里特定的事件为messagearrived ---步骤2*/
/*上面这语句值得注意的地方是 messagearrived方法被关联到messagehandler上后,以后消息的传递就通过messagehandler这个委托来实现,所以如果要能接收这个消息,就必须能支持messagehandler这个委托,也就是要有一个和委托一样的签名 
private timer polltimer;
public connection()
{
//
// todo: 在此处添加构造函数逻辑
//
polltimer=new timer(100);
polltimer.elapsed+=new elapsedeventhandler(checkformessage);
}

public void connect()
{
polltimer.start();
}

public void disconnect()
{
polltimer.stop();
}

public void checkformessage(object sender,elapsedeventargs e)
{
console.writeline("check for message.");
random random=new random();
if((random.next(9)==0)&&(messagearrived!=null))
{
messagearrived("hello mum!");//程序编写者自己产生一个消息,消息的内容为hello mum!
}
}
}
}

//====================display.cs===========
//接收事件的类,也就是上面提到的b
//=========================================
using system;

namespace ch12ex02
{
/// <summary>
/// display 的摘要说明。
/// </summary>
public class display
{
public display()
{
//
// todo: 在此处添加构造函数逻辑
//

}

public void displaymessage(string message) //a事件的最终处理函数,即上面的b.b,在main函数中,我们会使用本函数实现一个委托实例,并且添加到a的messagearrived事件列表中--步骤3
{
console.writeline("message arrived:{0}",message);
}
}
}

//====================class1.cs=================
//一个控制台可执行类,主要是使用上面两个类的实例
//==============================================
using system;

namespace ch12ex02
{
/// <summary>
/// class1 的摘要说明。
/// </summary>
class class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main(string[] args)
{
//
// todo: 在此处添加代码以启动应用程序
//

connection myconnection=new connection();
display mydisplay=new display();
myconnection.messagearrived+=new messagehandler(mydisplay.displaymessage);//把委托添加到当前a的事件列表中----步骤4和步骤5

myconnection.connect();
console.readline();
}
}
}

值得注意的代码:
public delegate void messagehandler(string messagetext);//委托定义
public event messagehandler messagearrived;//定义一个事件,并且关联到一个委托上
myconnection.messagearrived+=new messagehandler(mydisplay.displaymessage);//产生一个委托实例,并通过+=运算符号添加到事件列表中 +=运算符号在这里非常的有用 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网