Events (event) is a very important concept that we are triggering and receiving various events: mouse click event, keyboard event, and various events to handle the operating system. The so-called event is a message issued by an object. For example, the user presses a button, a file has changed, and there is data arrive on the socket. The object of the trigger event is called a sender (Sender), capturing an event and performs a response, called a Receiver, and an event can exist multiple recipients.
In asynchronous mechanisms, events are a very common way of communicating between threads. For example, the user presses a button on the interface to perform a certain task. The program initiates a thread to handle this task, and a schedule is displayed on the user interface to indicate the status of the user task. This feature can be processed using an event. You can send a "taskstart" event when the class is sent as a message, the "taskstart" event is issued, and the task "event is issued in different times, and the ratio of the parameter description task is carried out." Taskdone "Event, receive and process these events in the screen. This implements the functionality, and the interface and background execution tasks are also the lowest.
Specifically, the C # language, the implementation of the event relies on the concept of "delegate", first understand the agent.
DELEGATE
Delegate is a type in C #, which is actually a class that can hold a reference to a method. Unlike other classes, the Delegate class can have a signature, and it can only hold a reference to the method that matches its signature. Its implementation is very similar to the function pointers in C / C . It allows you to pass a class A method m to the object of another class B, so that the object of class B can call this method M. But than the function pointer, Delegate has many functions that have many function pointers. First, the function pointer can only point to the static function, and DELEGATE can reference the static function and can reference the non-statistial member function. When references the non-static member function, DELEGATE not only saves references to this function portal pointer, but also saves references to class instances that call this function. Second, DELEGATE is an object-oriented object-oriented, type, secure, and reliable control. That is, Runtime ensures that delegate points to an effective way, you don't have to worry that Delegate will point to invalid addend or base address.
Implementing a delegate is very simple, you can implement a delegate by the following three steps:
1. Declare a Delegate object that should have the same parameters and return value types as you want to pass.
2. Create a Delegate object and incorporate the function you want to pass as a parameter.
3. Call the method by the object to be created in the previous step.
Here is a simple example:
Public Class MyDelegateTestSt
{
// Step 1, declare the delegate object
Public delegate void mydelegate (String name);
// This is the way we want to pass, it has the same parameters and return value types with MyDelegate.
Public static void mydelegatefunc (string name)
{
Console.writeline ("Hello, {0}", name);
Public static void
Main
()
{
// Step 2, create a Delegate object
MyDelegate MD = New MyDelegate (MyDelegatetest.mydElegateFunc);
// Step 3, call Delegate
MD ("SAM1111");
}
}
The output result is: Hello, SAM1111
Let's take a look at how the event is handled:
Event (Event)
Event processing in C # is actually a delegate with a special signature, like the following:
Public Delegate Void MyEventHandler (Object Sender, MyEventArgs E);
Two of these parameters, Sender represents event senders, E is an event parameter class. MyEventargs class is used to include data related to events, and all event parameter classes must be derived from the System.Eventargs class. Of course, if your event does not include special parameters, you can use the System.Eventargs class directly as a parameter.
Combined with DELEGATE implementation, we can break the implementation of custom events to the following steps:
1: Define the delegate object type, it has two parameters, the first parameter is the event sender object, the second parameter is an event parameter class object.
2: Define the event parameter class, which should be derived from the System.Eventargs class. This step can be omitted if the event does not have a parameter.
3: Define an event handling method, which should have the same parameter and return value type with the Delegate object.
4: Define an event object with an Event keyword, which is also a delegate object.
5: Add events to the event queue with = operator (- = operator to delete events from the queue).
6: Write an event trigger method when you need to trigger events. In general, this method should be protected by protected, which cannot be called in public mode, but can be inherited by subclasses. The name can be oneventname.
7: Call the event trigger method to trigger the event at the appropriate place.
Below is an example, examples of mode template and control modes, triggering an event by control, capturing and processing in a container.
The trigger of the event:
///
///
Please Input 'A': a
Hello
Some Event Occur!
Event application
For example, there is a need for the following requirements: a sub-window pops up in the program main screen. At this time, the main screen can still receive the user's operation (the child window is non-modal). Some operations on the sub-window, depending on the result of the operation, different data is displayed on the main screen. I found some programmers to implement this function:
After the main screen pops up, the own pointer is given to the child screen, then use this pointer in the sub-picture, call the method provided by the main screen, and change the data display on the main screen. This can achieve a strong coupling between each module. Generally speaking that the call between the modules should be unilaterally: module A calls the module B, and the module B should not call A, otherwise the level of the program is destroyed, and the degree of coupling is enhanced, and the function changes and It's hard to add.
At this time, the correct approach should be a variety of events during the operation of the sub-window, and the main window captures these events to process, and each module is concentrated, do not need to ask other modules.
references
SAM111 BLOG: http://blog.9cbs.net/sam1111/ C # Delegate and Event