C#中的事件和委托

事件(event),这个词儿对于初学者来说,往往总是显得有些神秘,不易弄懂。而这些东西却往往又是编程中常用且非常重要的东西。大家都知道windows消息处理机制的重要,其实C#事件就是基于windows消息处理机制的,只是封装的更好,让开发者无须知道底层的消息处理机制,就可以开发出强大的基于事件的应用程序来。

先来看看事件编程有哪些好处。
在以往我们编写这类程序中,往往采用等待机制,为了等待某件事情的发生,需要不断地检测某些判断变量,而引入事件编程后,大大简化了这种过程:
- 使用事件,可以很方便地确定程序执行顺序。
- 当事件驱动程序等待事件时,它不占用很多资源。事件驱动程序与过程式程序最大的不同就在于,程序不再不停地检查输入设备,而是呆着不动,等待消息的到来,每个输入的消息会被排进队列,等待程序处理它。如果没有消息在等待,则程序会把控制交回给操作系统,以运行其他程序。
- 事件简化了编程。操作系统只是简单地将消息传送给对象,由对象的事件驱动程序确定事件的处理方法。操作系统不必知道程序的内部工作机制,只是需要知道如何与对象进行对话,也就是如何传递消息。

  有了这么多好处,看来我们的确有必要掌握它。俗话说:“难了不会,会了不难”。就让我们一步一步开始吧...

  要讲事件,必然要讲到委托(delegate)。它们之间的关系可以通过一个浅显的比方来说明,这个比方可能不是十分恰当。比如你要租一个房屋,这是一个事件,那么委托就是房屋租赁中介,当你把租房子的消息告知中介后,中介就会产生出一套符合你要求的房屋租赁方案来。再由中介执行这套方案,你便租得了这个房屋,即事件被处理了。当然你也可以不通过中介,直接找房东,但如果没有互联网等工具,你如何得到谁出租房屋的信息?话题扯远了。

Delegate (the delegate)
delegate can be appreciated that a function pointer, a delegate is a different object-oriented, and are type-safe. About commissioned understand, you can refer to my other article "personal understanding of C # delegate."

Events (event)
  we can put the event program simply divided into two parts: the type of event occurred (called the written event generator) and the event reception processing class. Class event occurs that is triggered by an event in this class, but the class does not know which object or method will add it to receive and process events triggered. What is needed is a medium exists between the sender and the receiver. The media in the .NET Framework is commissioned (delegate). In such an event receiving process, we need to have a method to handle the event. Well, we in this order to implement a program to capture keyboard keys, step by step instructions on how to write an event applications.

1, first create an own EventArgs class.
Quoted from the MSDN:
EventArgs comprising a base class event data, the event does not contain such data, when an event is not transmitted to the initiator event event handler may use this status information. If the event handler requires state information, the application must save the data derived from such a class.
Because in our keyboard events to be included in the key information, to derive a class KeyEventArgs to save key information, so that later to know which key to press.

class KeyEventArgs Internal: EventArgs
{
    Private char the keyChar;
    public KeyEventArgs (char the keyChar): Base ()
    {
        this.keyChar the keyChar =;
    }

    public char KeyChar
    {
        get
        {
            return keyChar;
        }
    }
}

2、再创建一个事件发生的类KeyInputMonitor,这个类用于监控键盘按键的输入并触发一个事件:
internal class KeyInputMonitor
{
    // 创建一个委托,返回类型为void,两个参数
    public delegate void KeyDown( object sender, KeyEventArgs e );
    // 将创建的委托和特定事件关联,在这里特定的事件为OnKeyDown
    public event KeyDown OnKeyDown;

    public void Run()
    {
        bool finished = false;
        do
        {
            Console.WriteLine( "Input a char" );
            string response = Console.ReadLine();

            char responseChar = ( response == "" ) ? ' ' : char.ToUpper( response[0] );
            switch( responseChar )
            {
                case 'X':
                    finished = true;
                    break;
                default:
                    // 得到按键信息的参数
                    KeyEventArgs keyEventArgs = new KeyEventArgs( responseChar );
                    // 触发事件
                    OnKeyDown( this, keyEventArgs );
                    break;
            }
        }while( !finished );
    }
}

这里注意OnKeyDown( this, KeyEventArgs );一句,这就是触发事件的语句,并将事件交由KeyDown这个委托来处理,委托指定事件处理方法去处理事件,这就是事件接收方的类的事情了。参数this是指触发事件的对象就是本身这个对象,keyEventArgs包含了按键信息。

3、最后创建一个事件接收方的类,这个类先产生一个委托实例,再把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件。然后提供一个方法回显按键信息。
internal class EventReceiver
{
    public EventReceiver( KeyInputMonitor monitor )
    {
        // 产生一个委托实例并添加到KeyInputMonitor产生的事件列表中
        monitor.OnKeyDown += new KeyInputMonitor.KeyDown( this.Echo );
    }
    private void Echo(object sender, KeyEventArgs e)
    {
        // 真正的事件处理函数
        Console.WriteLine( "Capture key: {0}", e.KeyChar );
    }
}

4、看一下如何调用
public class MainEntryPoint
{
    public static void Start()
    {
        // 实例化一个事件发送器
        KeyInputMonitor monitor = new KeyInputMonitor();
        // 实例化一个事件接收器
        EventReceiver eventReceiver = new EventReceiver( monitor );
        // 运行
        monitor.Run();
    }
}

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

C#中事件产生和实现的流程:
1.定义A为产生事件的实例,a为A产生的一个事件
2.定义B为接收事件的实例,b为处理事件的方法
3.A由于用户(程序编写者或程序使用者)或者系统产生一个a事件(例如点击一个Button,产生一个Click事件)
4.A通过事件列表中的委托对象将这个事件通知给B
5.B接到一个事件通知(实际是B.b利用委托来实现事件的接收)
6.调用B.b方法完成事件处理

转载于:https://www.cnblogs.com/baishiying/archive/2012/07/24/2606668.html

Guess you like

Origin blog.csdn.net/weixin_34050427/article/details/93439965