An article that implements custom event ... I haven't fully figured it out yet. I don't know if anyone's interested. Beginners do not have to see, first learn to walk ...

Source: Internet
Author: User
Tags count
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 C An IS data rich and work in a heterogeneous environment.

In this discussion we'll examine the steps to create a custom event that'll alert the user or process that event has T Aken place under a specified condition. To understand you to setup the code structure to raise and handle events, a 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 which 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 is registered with an event sender.


The fundamental definition of the event Is:an event is a message this has been sent by an object due to some occurrence of An action within the workflow of the application. This action could is a button that has been clicked or specified timeout parameter value has been. 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 would handle the event. Therefore, an agent must is utilized to handle the communication. This is the delegate class, holds a reference to the "method" that handles the event. The delegate class has a intrinsic signature and can only hold references to methods that obtain the same. 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 is inherited from System.EventArgs.
-A delegate for the event.
-A class that raises the event.
-A class with a is that handles the event.


Now so we have discussed the fundamentals of event handling, we need a simple application to depict the event functional ity.

Custom Events Application

The Custom Events application is comprised to 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 are created to count to some number and stop. During This count, a specific value is interrogated within the loop. When the specified value was 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 of the is called and the event is raised. Note:the initevent () method was called from Page_Load () to the Page is requested. The Initevent () 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-certainly overkill to display a line of the text in a label field on a Web page. However, it is useful to examine the workflow of the "an event" 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 the event comprise . Below in Listing 3 shows the respective components, that'll 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 would supply the data for event. Notice that it's inherited from EventArgs. As you can, the MessageText is a, the class and a Get statement are utilized to return the data string. The delegate in the MessageHandler. It has the sender object and the Messageeventargs for the ' data in its argument list. Moving onto the class messageevent. This class would raise the event with the OnMessage () method by invoking the delegates. The Messaginghandler class would extract the event message from the Messageeventargs and populate of the Eventmessage property So the message can is displayed on the Web page.

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

-The Initevent () is initiated from the Page_Load () to 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 this Messaginghandler instance can receive alarm Events. The + + assignment operator adds the delegate to the "List of delegates that are" with the "message event."
-When the specified value is reached, Messageeventargs e = new Messageeventargs () are 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" Messageeventargs and populates the Eventmessage property W ith 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 and the handling of events are typically server-side the event mes Sage is displayed after the entire code sequence has transpired. This paradigm isn't conducive for asynchronous processing due to the fire-and-forget. However, they can be useful if to you are the doing heavy server-side processing and validation before the page.

The. NET Framework is providing the application developer a great deal of options to generate meaningful to the Enterprise, be it Web based applications, Windows Client based applications, or Server based Services


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.