Net Custom Event Summary
I 've thought a lot about my work recently. I 'd like to skip three thousand words here...
Let's take the event technology and accumulate it. Continue to learn and improve yourself. Well, let's get started with things ~
You should be familiar with the incident.ProgramMembers can pat their chests and say "event? I know ~", How can I customize events? Maybe someone is not so confident. Let's just follow the old rules ~
Let's use an example that has been cited in many places to write it. The author will not go into it. The description is as follows: when a thief enters, the event sender (CAT) sends a cat call event (custom), and the (event receiver) Owner subscribes to the cat call event and processes it (captures the thief ), the event parameters pass the number of thieves.
The specific steps for implementation are as follows:CodeIt is marked in the comment for you to abstract the process of custom events.
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Namespace Custom Event { // Http://www.cnblogs.com/DebugLZQ // Sample Code for customizing event steps // 0. Define parameters passed by the event Class Alarmeventargs: eventargs { Public Int Numberofthief; Public Alarmeventargs ( Int N) {numberofthief = N ;}} // Event sender Class Dog { // 1. Declare the event Delegate Public Delegate Void Alarmeventhandler (Object sender, alarmeventargs E ); // 2. Declare events Public Event Alarmeventhandler alarm; // 3. functions that trigger the event Public Void Onalarm (alarmeventargs e ){ If ( This . Alarm! = Null ) {Console. writeline ( " The dog is Wangwang... " ); This . Alarm ( This , E );}}} // Event subscriber Class Host { // 4. subscribe to events Public Host (Dog dog) {dog. Alarm + = New Dog. alarmeventhandler (hosthandleevent );} // 5. Event Handler Public Void Hosthandleevent (Object sender, alarmeventargs e) {console. writeline ( " Host caught the thief! The sum is " + E. numberofthief );}} Class Program { Static Void Main ( String [] ARGs) {console. writeline ( " Program is running... " ); Dog = New Dog (); host = New Host (DOG); console. writeline ( " Someone is coming... " ); // 6. trigger events when appropriate Alarmeventargs = New Alarmeventargs ( 3 ); Dog. onalarm (alarmeventargs); console. readkey ();}}}
The program running result is as follows:
The example is simple.
I 've thought a lot about my work recently. I 'd like to skip three thousand words here...
Let's take the event technology and accumulate it. Continue to learn and improve yourself. Well, let's get started with things ~
Everyone should be familiar with the incident. I believe every programmer can pat his chest and say, "What is the event? I know ~", How can I customize events? Maybe someone is not so confident. Let's just follow the old rules ~
Let's use an example that has been cited in many places to write it. The author will not go into it. The description is as follows: when a thief enters, the event sender (CAT) sends a cat call event (custom), and the (event receiver) Owner subscribes to the cat call event and processes it (captures the thief ), the event parameters pass the number of thieves.
The specific steps for implementation are as follows: it has been marked in the code comment for you to abstract the process of customizing events.
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Namespace Custom Event { // Http://www.cnblogs.com/DebugLZQ // Sample Code for customizing event steps // 0. Define parameters passed by the event Class Alarmeventargs: eventargs { Public Int Numberofthief; Public Alarmeventargs ( Int N) {numberofthief = N ;}} // Event sender Class Dog { // 1. Declare the event Delegate Public Delegate Void Alarmeventhandler (Object sender, alarmeventargs E ); // 2. Declare events Public Event Alarmeventhandler alarm; // 3. functions that trigger the event Public Void Onalarm (alarmeventargs e ){ If ( This . Alarm! = Null ) {Console. writeline ( " The dog is Wangwang... " ); This . Alarm ( This , E );}}} // Event subscriber Class Host { // 4. subscribe to events Public Host (Dog dog) {dog. Alarm + = New Dog. alarmeventhandler (hosthandleevent );} // 5. Event Handler Public Void Hosthandleevent (Object sender, alarmeventargs e) {console. writeline ( " Host caught the thief! The sum is " + E. numberofthief );}} Class Program { Static Void Main ( String [] ARGs) {console. writeline ( " Program is running... " ); Dog = New Dog (); host = New Host (DOG); console. writeline ( " Someone is coming... " ); // 6. trigger events when appropriate Alarmeventargs = New Alarmeventargs ( 3 ); Dog. onalarm (alarmeventargs); console. readkey ();}}}
The program running result is as follows:
The example is simple.