A simple description of the event. msdn)

Source: Internet
Author: User

Simple event explanation: (from msdn)
An event is a message sent by an object to send a notification. Operations may be caused by user interaction (such as mouse clicking), or by some other program logic. The object that triggers an event is called the event sender. The object that captures an event and responds to it is called the event receiver.

In event communication, the event sender class does not know which object or method will receive (process) the event it raises. It is necessary to have a media (or pointer-like mechanism) between the source and the receiver ).. Net Framework defines a special type (delegate), which provides the function pointer function.

Unlike other classes, the delegate class has a signature and can only reference methods that match the signature. In this way, the delegate is equivalent to a type security function pointer or a callback.

Steps required to use the event in C:
1. Create a delegate
2. Associate the created delegate with a specific event(. Many events in the. NET Class Library have been customized, so they have a corresponding delegate, when writing the associated Event Handler-that is, when an event occurs, we need to execute the method with the same signature as this delegate)
3. Write an event handler
4. Use the compiled event handler to generate a delegate instance

5. Add the delegated instance to the event list that generates the event object. This process is also called subscription event.
Process for event generation and implementation in C:
1. Define a as the instance that generates the event, and a as an event generated by
2. Define B as the instance for receiving events, and B as the method for handling events.
3. A a user (program writer or program user) or the system generates an A event (for example, clicking a button to generate a click event)
4. A notifies B of this event through the delegate object in the event list

5. B receives an event notification (B. B uses delegation to receive the event)
6. Call Method B to complete event processing.

The example of "C # Getting Started classic" is given below, and some explanations are given:
// ================================= Connection. CS ================
// Event definition, that is, a mentioned above
// ================================================ ======
Using system;
Using system. Timers;

Namespace ch12ex02
{
/// <Summary>
/// Summary of connection.
/// </Summary>
///

Public Delegate void messagehandler (string messagetext); // create a delegate --- Step 1
Public class connection
{
Public event messagehandler messagearrived; // associate the created delegate with a specific event. The specific event here is messagearrived --- Step 2 */
/* The preceding statement is worth noting that after the messagearrived method is associated with messagehandler, the message will be passed through the messagehandler delegate. Therefore, if you want to receive the message, the messagehandler delegate must be supported, that is, there must be a signature that is the same as the delegate.
Private timer polltimer;
Public connection ()
{
//
// Todo: add the constructor logic here
//
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! "); // The program writer generates a message. The message content is hello mum!
}
}
}
}

// ================================== Display. CS ==================
// Class for receiving events, that is, B mentioned above
// ================================================ ===
Using system;

Namespace ch12ex02
{
/// <Summary>
/// Display abstract description.
/// </Summary>
Public class display
{
Public Display ()
{
//
// Todo: add the constructor logic here
//

}

Public void displaymessage (string message) // The final processing function of the event, that is, the preceding B. b. In the main function, we will use this function to implement a delegate instance and add it to the messagearrived event list of A-Step 3
{
Console. writeline ("message arrived: {0}", message );
}
}
}

// ================================= Class1.cs ======================
// A console executable class, mainly used for instances of the above two classes
// ================================================ ==========
Using system;

Namespace ch12ex02
{
/// <Summary>
/// Summary of class1.
/// </Summary>
Class class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
//
// Todo: Add code here to start the application
//

Connection myconnection = new connection ();
Display mydisplay = New Display ();
Myconnection. messagearrived + = new messagehandler (mydisplay. displaymessage); // Add the delegate to the event list of current A-steps 4 and 5

Myconnection. Connect ();
Console. Readline ();
}
}
}

Note: The Event definition section in the program code. For more information about the delegate definition section, see msdn.
Code worth noting:
Public Delegate void messagehandler (string messagetext); // delegate Definition
Public event messagehandler messagearrived; // defines an event and associates it with a delegate.
Myconnection. messagearrived + = new messagehandler (mydisplay. displaymessage); // generate a delegate instance and add it to the event list using the plus sign (+ =)

Author's blog:Http://blog.csdn.net/juky_huang/

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.