一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会

The latest offering from Microsoft to support software development is the .NET Framework. Inside this vast Framework is the ASP.NET. The ASP.NET facilitates the application development community in generating high performance Web based applications that can be data rich and work in a heterogeneous environment.

In this discussion we will examine the steps to create a custom event that will alert the user or process that event has taken place under a specified condition. To understand how to setup the code structure to raise and handle events, an overview of the event architecture is needed.

Event Architecture

Below are a number of key points that encompass the .Net Framework event model.

- Events taking place in the .NET Framework are based upon the delegate model.
- The delegate class is the mechanism that allows the event sender to communicate with the event handler.
- An event sender can broadcast an event to a number of event handlers.
- An event handler must be registered with an event sender.

The fundamental definition of an event is: An event is a message that has been sent by an object due to some occurrence of an action within the workflow of the application. This action could be a button that has been clicked or specified timeout parameter value has been reached. This action triggers the event and the subsequent method that handles the event is initiated and the event is handled.

In the .NET framework, the sending object does not know what method will handle the event. Therefore, an agent must be utilized to handle the communication. This agent is the delegate class that holds a reference to the method that handles the event. The delegate class has an intrinsic signature and can only hold references to methods that obtain the same signature. You can equivocate the delegate class to a type-safe pointer to a callback. The event delegate has two parameters; these are the sending object and the returned data. Below in Listing 1 is generic declaration of the event delegate class.

Listing 1

Public delegate void EventHandler(object Sender, EventArgs e);

In order to initiate and handle custom events, event functionality needs to be provided. The fundamental structure is listed as follows:

- A class to hold the event data. This must be inherited from System.EventArgs.
- A delegate for the event.
- A class that raises the event.
- A class with a method that handles the event.

Now that we have discussed the fundamentals of event handling, we need a simple application to depict the event functionality.

Custom Events Application

The Custom Events Application is comprised of a simple Web Form that displays the event data message. This is shown in Figure 1.

Figure 1

In order to initiate some action that shows the functionality of event handling, a loop is created to count to some number and stop. During this count, a specific value is interrogated within the loop. When the specified value is equal to the loop value, the event is fired. In this case the value is 400000. The loop runs from 1 to 500000. When the value 400000 is reached the OnMessage method is called and the event is raised. Note: The InitEvent() method is called from Page_Load() method when the page is requested. The InitEvent() method is depicted in Listing 2.

Listing 2

private void InitEvent()
  {
    MessagingHandler mh = new MessagingHandler();
    MessageEvent me = new MessageEvent();
    me.Message += new MessageHandler(mh.GetMessageData);

    for (int i = 1; i <= 500000; i++)
    {
        if (i == 400000)
        {
          MessageEventArgs e = new MessageEventArgs();
          me.OnMessage(e);
          lblMessage.Text = mh.EventMessage;
        }
    }
  }

As you can see this is certainly overkill to display a line of text in a label field on a Web page. However, it is useful to examine the workflow of an event that is captured and handled in the .NET Framework. In order to make sense of the code fragment in Listing 2, we need to examine the classes that comprise the event structure. Below in Listing 3 shows the respective components that will raise and handle the event as it transpires.

Listing 3

public class MessageEventArgs : EventArgs
{
    public string MessageText
    {
        get
        {
            return ("There has been 400000 values processed.");
        }
    }
}

public delegate void MessageHandler(object sender, MessageEventArgs e);

public class MessageEvent
{
    public event MessageHandler Message;

    public void OnMessage(MessageEventArgs e)
    {
        if (Message != null)
        {
            Message(this, e);
        }
    }
}

public class MessagingHandler
{
    private string strMessage;
    public string EventMessage
    {
        get
        {
            return strMessage;
        }
        set
        {
            strMessage = value;
        }
    }

    public void GetMessageData(object sender, MessageEventArgs e)
    {
        string strMessage = e.MessageText;
        EventMessage = strMessage;
    }
}

The class MessageEventArgs will supply the data for event. Notice that it is inherited from EventArgs. As you can see MessageText is a property of the class and a get statement is utilized to return the data string. The delegate in this case is the MessageHandler. It has the sender object and the MessageEventArgs for the data in its argument list. Moving onto the class MessageEvent. This class will raise the event with the OnMessage() method by invoking the delegates. The MessagingHandler class will extract the event message from the MessageEventArgs and populate the EventMessage property so the message can be displayed on the Web page.

In summarization, we have a class that supplies the data, a delegate to provide the communication, a class to raise the event, and a class to handle the event. The basic workflow is as follows:

- The InitEvent() is initiated from the Page_Load() method when the page is requested.
- MessagingHandler mh = new MessagingHandler() instantiates the MessagingHandler class - Event Receiver.
- MessageEvent me = new MessageEvent() instantiates the MessageEvent class - Event Sender.
- me.Message += new MessageHandler(mh.GetMessageData) registers the event handler with the event source so that the MessagingHandler instance can receive alarm events. The += assignment operator adds the delegate to the list of delegates that are registered with the Message event.
- When the specified value is reached, MessageEventArgs e = new MessageEventArgs() is instantiated and the OnMessage() is called to raise the event.
- The OnMessage() method invokes the delegate which calls the GetMessageData() method to handle the event.
- The GetMessageData() method obtains the message text from the MessageEventArgs and populates the EventMessage property with the string data.
- The label field is populated with the EventMessage string property value and displayed on the Web page.

Run this example

[ run it ]

Conclusion

Since Web based applications are disconnected in nature and the handling of events are typically server-side the event message is displayed after the entire code sequence has transpired. This paradigm is not conducive for asynchronous processing due to the fire-and-forget processing. However, they can be useful if you are doing heavy server-side processing and validation before rendering the page.

The .NET Framework is providing the application developer a great deal of options to generate meaningful solutions to the enterprise, be it Web based applications, Windows Client based applications, or Server based Ser
文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会-摸透电贝司">

时间: 2024-09-12 06:35:46

一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会的相关文章

spring中自定义Event事件的使用和浅析

在我目前接触的项目中,用到了许多spring相关的技术,框架层面的spring.spring mvc就不说了,细节上的功能也用了不少,如schedule定时任务.Filter过滤器. interceptor拦截器等等,而这一篇我要说的是spring Event自定义事件,目前的项目中似乎没怎么用,但是这一项技术貌似还蛮重要,所以也不能不掌握. 对于事件驱动模型的解释和理解,我觉得有一篇博客里说的非常好,尤其是在解释这个关系的时候,举的交通信号灯的例子非常贴切,这里就引用做一个简单的解释: 事件驱

如果看了这篇文章你还不懂傅里叶变换,那就过来掐死我吧(下)

上一篇文章发出来之后,为了掐死我,大家真是很下工夫啊,有拿给姐姐看的,有拿给妹妹看的,还有拿给女朋友看的,就是为了听到一句"完全看不懂啊".幸亏我留了个心眼,不然就真的像标题配图那样了.我的文章题目是,如果看了这篇文章你"还"不懂就过来掐死我,潜台词就是在你学了,但是没学明白的情况下看了还是不懂,才过来掐死我. 另外,想跟很多人抱歉,因为评论太多了,时间有限,不能给每个人回复,还望大家谅解.但是很感谢一直在评论区帮忙解答读者问题的各位,就不一一@了. 这里郑重感谢大

求救-非常急!想程序连续5黑或5白自动出现一个对话框,显示我赢了还可以选继续或退出游戏

问题描述 非常急!想程序连续5黑或5白自动出现一个对话框,显示我赢了还可以选继续或退出游戏 import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.*;import java.awt.*;public class Example2 ext

求资源- 求一个简单自定义的框,右上角需要关闭框的叉。最简单的,不需要样式的。

问题描述 求一个简单自定义的框,右上角需要关闭框的叉.最简单的,不需要样式的. 我需要自己写一个框,里面可以写内容,最简单的就行.可以不需要样式. 解决方案 easyDialog v2.0 http://stylechen.com/easydialog-v2-0.html 解决方案二: artDialog:http://demo.jb51.net/js/2011/artDialog/_doc/labs.html 解决方案三: 不需要插件的有没,纯自己写的那种,最简单的就可以.

面试了几家公司 一个公司是做arcgis做地图 还一家是用高德地图做 那个有前途

问题描述 面试了几家公司一个公司是做arcgis做地图还一家是用高德地图做那个有前途高德地图和arcgis地图那个有前途去那边好??有相关行业的大牛指点下谢谢! 解决方案 解决方案二:不如来中地吧解决方案三:你做gis开发多久,侧重什么技术.解决方案四:现在是做arcgisforjavascriptapi开发已经开发一个月了哈哈解决方案五:一楼高级黑???解决方案六:引用1楼chenman124的回复: 不如来中地吧 高级黑解决方案七:可以去高德那边看看

项目中布局文件怎么引用另一个项目自定义的组件

问题描述 项目中布局文件怎么引用另一个项目自定义的组件 如果我在一个项目里自定义了一个组件,我想在另一个项目的布局文件中加入这个组件,怎么配置呢?怎么让布局文件能够找到这个组件呢? 解决方案 参考这个例子:http://www.yiibai.com/android/simple_android_custom_component_using_xm.html 解决方案二: xml中include

怎么让一个下拉框能编辑,还能绑定数据呢???

问题描述 怎么让一个下拉框能编辑,还能绑定数据呢???怎么让一个下拉框能编辑,还能绑定数据呢???怎么让一个下拉框能编辑,还能绑定数据呢???怎么让一个下拉框能编辑,还能绑定数据呢??? 解决方案 解决方案二:能编辑http://js.alixixi.com/a/2016407363445.shtmlhttp://vakinge.iteye.com/blog/754260解决方案三:有没有这个的JQ插件呢?

php生成缩略图类,支持自定义高和宽。还支持按高和宽截图

php教程生成缩略图类,支持自定义高和宽.还支持按高和宽截图 <?php  class resizeimage  {      //图片类型      var $type;      //实际宽度      var $width;      //实际高度      var $height;      //改变后的宽度      var $resize_width;      //改变后的高度      var $resize_height;      //是否裁图      var $cut; 

以下我写的一个javascript的web worker,但不知道为什么没数据返回了.

问题描述 以下我写的一个javascript的web worker,但不知道为什么没数据返回了. <!DOCTYPE html> function post(){ var wo1=new Worker("wroker1.js"); var a=document.getElementById("input"); wo1.postMessage(a.value); wo1.onmessage=function(event){ document.getElem