event, this word for beginners, often always seem a little mysterious, difficult to understand. And these things are often used in programming and very important things. We all know the importance of the Windows message processing mechanism, in fact, the C # event is based on the Windows message processing mechanism, but the encapsulation is better, so that developers need not know the underlying message processing mechanism, can develop a powerful event-based application.
Let's take a look at the benefits of event programming.
In the past we wrote such programs, often using the wait mechanism, in order to wait for something to happen, the need to constantly detect some of the judgment variables, and the introduction of event programming, greatly simplifies the process:
-Use events to make it easy to determine the order in which programs are executed.
-When an event driver waits for an event, it does not consume a lot of resources. The biggest difference between an event driver and a procedural program is that the program no longer checks the input device, but stays still, waits for the message to arrive, and each incoming message is queued and waits for the program to process it. If no messages are waiting, the program returns control to the operating system to run other programs.
-Events simplify programming. The operating system simply passes the message to the object, which determines how the event is handled by the object's event driver. The operating system does not have to know how the program works inside, but it needs to know how to talk to the object, that is, how to deliver the message.
With so many benefits, it seems that we really need to master it. As the saying goes: "No, it will not be difficult." Let's start with a step-by-step ...
To tell an event, it is necessary to refer to the Commission (delegate). The relationship between them can be illustrated by an obvious analogy, which may not be quite appropriate. For example, you want to rent a house, this is an event, then the Commission is a housing leasing intermediary, when you rent the house news to inform the intermediary, the intermediary will produce a set of requirements to meet your housing leasing scheme. Then by the intermediary to carry out this package, you can rent this House, that the incident was dealt with. Of course, you can also not through the intermediary, directly to the landlord, but if there is no Internet tools, how do you get the information about who rented the house. The topic is far away.
Delegate (delegate)
A delegate can be understood as a function pointer, unlike a delegate that is object oriented and type-safe. About the delegation of understanding, you can refer to my another article, "C # Commissioned by the personal understanding."
Event
We can simply divide the event programming into two parts: the class that the event takes place (written on the event generator) and the class that the event receives processing. The class in which the event occurs means that an event is triggered in this class, but the class does not know which object or method will receive and handle the event that it triggers. What is needed is a medium between the sender and the receiver. This medium is the delegate (delegate) in the. NET framework. In the class where event reception is handled, we need to have a way of handling events. OK, so we're going to implement a program that captures keyboard keys in this order, step-by-step instructions on how to write an event application.
1. First, create a EventArgs class of your own.
Quote from MSDN:
EventArgs is the base class for classes that contain event data, and this class does not contain event data, which is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data.
Because in our keyboard key event to include key information, so to derive a KeyEventArgs class, to save the key information, so that the following know what button pressed. Internal class Keyeventargs:eventargs
{
Private char Keychar;
Public KeyEventArgs (char Keychar): Base ()
{
This.keychar = Keychar;
}
Public Char Keychar
{
Get
{
return Keychar;
}
}
}
2. Create an event class Keyinputmonitor that monitors the input of keyboard keys and triggers an event: internal class Keyinputmonitor
{
Create a delegate with a return type of void, two parameters
public delegate void Keydownhandler (object sender, KeyEventArgs e);
Associates the created delegate with a specific event where the specific event is KeyDown
public event Keydownhandler KeyDown;
public void Run ()
{
bool finished = false;
Todo
{
Console.WriteLine ("Input a char");
String response = Console.ReadLine ();
Char Responsechar = (Response = = "")? ': Char. ToUpper (Response[0]);
Switch (Responsechar)
{
Case ' X ':
Finished = true;
Break
Default
Get the parameters of the key information
KeyEventArgs KeyEventArgs = new KeyEventArgs (Responsechar);
Triggering events
KeyDown (this, KeyEventArgs);
Break
}