Message processing mechanism in C# (event and delegation)

Anyone who has written Windows desktop applications knows that most of the communication between Microsoft's Windows operating system and applications is based on the message loop mechanism. In VC++, the program uses the GetMessage, TranslateMessage, DispatchMessage statements to get messages from the message queue, convert the messages and distribute them to the target window's procedure function, and the procedure function handles different Windows messages separately.

When you turn the development platform to C#, because C# encapsulates the message in object-oriented, the message is encapsulated into an event, which makes our understanding of the message transmission mechanism clouded. Let's start with the actual code. Let's start, take a step by step, and gradually understand the message processing mechanism in C# WinForm. When we create a new WinForm program in Program.cs, we see Application.Run(new Form1()); The Application class has functions for starting and stopping applications and Threads and methods for handling Windows messages. Through this statement, the current thread starts to run the standard application message loop and makes the specified window visible. The message loop is encapsulated into the Run() static method of the Application class. When the program ends, call Exit or ExitThread to stop the message loop, and the program terminates.  

We create the mouse click event MyClick for the Form1 form, and the program automatically adds some code for us. this.MouseClick+=newSystem.Windows.Forms.MouseEventHandler(this.MyClick); the above this.MouseClick is the mouse click event defined in C#. It is defined as: public event MouseEventHandler MouseClick; and the definition of MouseEventHandler is publicdelegate void MouseEventHandler(object sender, MouseEventArgs e); This statement defines a delegate named MouseEventHandler, what is a delegate? The delegate type can be understood as a function pointer in C++, but the difference is that the delegate is completely object-oriented and encapsulates the instance and method of the object. In essence, the delegate encapsulates an instance and the method functions on the instance into a callable entity, which is object-oriented and safe. We can understand the first sentence of code as this.MouseClick event adds a pointer to MyClick handler A function pointer to a function. The event handling method in C# needs to use the delegate type to register the event. Of course, you can also call this statement again to add another handler function such as MyClick2 to the event, and these processing of the event will form a handler function list. Then the MyClick2 function will continue to run after the MyClick function is completed after the mouse is clicked during the program running.

From the above, we can roughly guess its packaging process: 

The Application class extracts the message sent by the object from the application message queue, distributes it to the corresponding form, and converts it into an event. NET Framework defines a special type (Delegate) that provides the functionality of function pointers. In this way, the delegate is equivalent to a type-safe function pointer or a callback function. In C#, the event is associated with the function address of the response function through the Delegate delegation mechanism, and a list of function pointers is formed. When a message arrives, these response functions can be called one by one through the list of function pointers. We customize an event trigger by the following method to verify our above guess. We implement custom proxy events by manually adding code (same as custom messages in VC++) 

1. Declare event delegation. Here int para is only for convenience and represents the required parameter list, but it should be noted that the parameter list needs to be unified with the parameter list in step 3. public delegate void MyEventHandler(int para);

 2. Declare the event. The event keyword is used to declare the event in the publisher class, and delegate MyEventHandler as the type of the event. public event MyEventHandler MyEvent;

3. Add event handlers (methods that respond to events). public void OnMyEvent(int para) { MessageBox.Show(""Event trigger, parameter is:""+para);}

 4. Bind the specified event handler to the event to be processed (subscribing to the event). Note that this statement needs to be written in the program execution statement, such as the Form_Lord function. this.MyEvent += new MyEventHandler(OnMyEvent);

 5. Trigger the event (call the trigger method of the event). MyEvent(3); 

 6. Execute the event handler we need through the callback of the event delegate. A message box "Event triggered, parameter is: 3" pops up.

 The above experiments can be concluded that after the message in C# is converted into an event through the Application, the corresponding relationship between the event and the handler is completed through the above 6 steps. After the user triggers the event, the corresponding time handler is accurately executed. User-defined events can also be added through the above methods.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040895&siteId=291194637