C # Event Instance two
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 input message is queued and waits for the program to process it. If no messages are waiting,
The program will turn control back to the operating system to run other programs.
The operating system simply passes the message to the object, and the object's event driver determines how the event is handled. The operating system does not have to know the internal working mechanism of the program, just need to know how to talk to the object, that is, how to deliver the message.
Let's start by looking at the benefits of event programming.
1. Using events makes it easy to determine the order in which the programs are executed.
2. When an event driver waits for an event, it does not consume a lot of resources.
3. The event simplifies programming.
Events (Event)
We can simply divide the event programming into two parts: the class in which the event occurs (called the event generator in writing) and the class that the event receives processing . The class in which an 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 it fires. What is needed is a medium between the sender and the receiver. This medium is delegated (delegate) in the. NET framework. In the class where the event is received, we need to have a way to handle the event. Well, let's follow this order to implement a program that captures the keyboard keys, step-by-step instructions on how to write the event application.
namespaceeventlearn001{/*because we want to include the key information in our keyboard key event, we want to derive a KeyEventArgs class to save the key information so that we know which key to press **/ Internal classKeyeventargs:eventargs {Private CharKeyChar;//Private Fields PublicKeyEventArgs (CharKeyChar):Base() { This. KeyChar =KeyChar; } Public CharKeyChar//Exposing Properties { Get { returnKeyChar; } } } //then create an event class Keyinputmonitor, which is used to monitor keyboard input and punish an event; Internal classKeyinputmonitor {//create a delegate with a return type of void, two parameters Public Delegate voidKeydownhandler (Object sender, KeyEventArgs e); //the created delegate and the specific event association, Public EventKeydownhandler KeyDown; Public voidRun () {BOOLFinished =false; Do{Console.WriteLine ("Input a char"); stringResponse =Console.ReadLine (); CharResponsechar = (Response = ="")?' ':Char. ToUpper (response[0]); Switch(Responsechar) { Case 'x': finished =true; Break; default: //parameters to get key informationKeyEventArgs args =NewKeyEventArgs (Responsechar); //Departure EventsKeyDown ( This, args); Break; //KeyDown (this, KeyEventArgs); Here is the statement that triggered the event, and the event is referred to the Keydownhandler delegate for processing,//The delegate specifies the event handling method to handle the event,//This is the event receiver's class thing. //The parameter this refers to the object that triggered the event, which is the object itself, and KeyEventArgs contains the key information } } while(!finished); } } //Finally, create a class for the event receiver, which first produces a delegate instance,//Add this delegate instance to the event list that produces the event object, which is called subscribing to the event. Then provide a method to echo the key information. Internal classEventreceiver { Publiceventreceiver (Keyinputmonitor monitor) {Monitor. KeyDown+=OnKeyDown; } Private voidOnKeyDown (Object sender, KeyEventArgs e) {keyinputmonitor obj= (keyinputmonitor) sender;//He can be coerced into other uses;Console.WriteLine ("Capture key {0}", E.keychar); } } classProgram {Static voidMain (string[] args) { //instantiate an event sender;Keyinputmonitor monitor =NewKeyinputmonitor (); //Instantiate a receiverEventreceiver Eventreceiver =NewEventreceiver (monitor); Monitor. Run (); } }}
C # Event Instance two