event handling in C # is actually a delegate with a special signature, like this:
public delegate void MyEventHandler (object sender, MyEventArgs e);
Of the two parameters, sender represents the event sender, and E is the event argument class. The MyEventArgs class is used to contain event-related data, and all event argument classes must derive from the System.EventArgs class. Of course, if your event does not contain parameters, you can use the System.EventArgs class directly as a parameter.
It's that simple, combined with the implementation of delegate, we can boil down the implementation of custom events to the following steps:
1. Defines the delegate object type, which has two parameters, the first parameter is the event sender object, and the second parameter is the event argument class object.
2. Defines the event argument class, which should derive from the System.EventArgs class. This step can be omitted if the event has no parameters.
3. Define the event-handling method, which should have the same parameters and return value type as the delegate object.
4. Defines an event object with the Events keyword, which is also a delegate object.
5. Add events to the event queue with the + = operator (the-= operator can remove events from the queue).
6. Write the event-triggering method in the same place where the event needs to be triggered by calling delegate. In general, this method should be a protected access restriction that cannot be called public, but can be inherited by a quilt class. The name is OnEventName.
7. invoke the event-triggering method to trigger the event at the appropriate place.
Now let's write a program for custom events. The master kept a faithful watchdog, and the dog was in charge of the house at night when the master slept. Once a thief comes in, the dog sends out a alarm event, and the host will take corresponding action after receiving the alarm incident. Suppose the thief arrives at midnight on 2009 New Year's Day.
Event Sender
Class dog{//1.declaring a delegate about an event; public delegate void AlarmEventHandler (object sender, EventArgs e); 2.declaring Events; public eventAlarmEventHandlerAlarm; 3.write the function that raises the event; public voidOnalarm() {if (this. Alarm = null) {Console.WriteLine ("/N dog alarm: A thief came in, Barking ~~~~~~~"); This. Alarm (This, new EventArgs ()); Issue an alert}}}//Event RecipientsClass host{//4.Writing event handlersvoid Hosthandlealarm (object sender, EventArgs e) {Console.WriteLine ("Master: Caught the thief! "); }//5.Registering event HandlersPublic Host (dog dog) {dog. Alarm + = newDog.alarmeventhandler (hosthandlealarm); }}//6.now to trigger the eventClass program{static void Main (string[] args) {Dog dog = new Dog (); Host host = new host (dog); The current time, starting from December 31, 2008 23:59:50, is timed datetime now = new DateTime (2008, 12, 31, 23, 59, 50); DateTime midnight = new DateTime (2009, 1, 1, 0, 0, 0); Waiting for the arrival of Midnight Console.WriteLine ("Time is passing by seconds ... "); while (present < midnight) {Console.WriteLine ("Current time:" + now); System.Threading.Thread.Sleep (1000); The program pauses for one second now = now. AddSeconds (1); Time increases by one second}//Midnight 0 o'clock the thief arrives, the watchdog raises the alarm event Console.WriteLine ("/N Month Midnight:" + now); Console.WriteLine ("The thief crept into the master's house ... "); Dog. Onalarm (); }}
when the thief arrives at midnight, the dog calls dog. Onalarm () function, which triggers the alarm event, the system finds and executes the event handler Hosthandlealarm () registered in the Alarm event.
Event handling Delegate habits in EventHandler end , such as AlarmEventHandler. Events Alarm is actually Event Handling Delegate AlarmEventHandler an instance of . The code that raises the event is often written as a function, andthe. NET Convention has the name "oneventname"for this function, such as the function of Onalarm (). In the host class, we define the event handler Hosthandlealarm () and register it with the dog. The alarm event.
Relationship of delegates to events in C #