Net Custom Event summary to net Custom Event Summary

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.