C # (event triggering) callback function, perfect handling all kinds of incurable diseases!

Source: Internet
Author: User

Every time you write a blog, the first sentence is this: the programmer is very hard, in addition to writing procedures, but also to write a blog!

That's a lot of crap.

First: What is the case with a callback function?

By code: Give a simple example

//Event Sender    classDog {//1. Declaration of the Commission on the event;         Public Delegate voidAlarmEventHandler (Objectsender, EventArgs e); //2. Declaration of events;         Public EventAlarmEventHandler Alarm; //3. Write the function that raises the event;         Public voidOnalarm () {if( This. Alarm! =NULL) {Console.WriteLine ("\ n The dog called the Police: A thief came in, barking ~~~~~~~");  This. Alarm ( This,NewEventArgs ());//issue an alert            }        }    }    //Event Recipients    classHost {//4. Writing Event handlers        voidHosthandlealarm (Objectsender, EventArgs e) {Console.WriteLine ("master: Caught the thief! "); }        //5. Registering Event Handlers         PublicHost (dog dog) {dog. Alarm+=NewDog.alarmeventhandler (hosthandlealarm); }    }    //6. Now to trigger the event    classProgram {Static voidMain (string[] args) {Dog Dog=NewDog (); Host host=NewHost (dog); //current time, starting from December 31, 2008 23:59:50DateTime now =NewDateTime ( -, A, to, at, -, -); DateTime Midnight=NewDateTime ( .,1,1,0,0,0); //waiting for the midnight to comeConsole.WriteLine ("time passes by one second ...");  while(Now <midnight) {Console.WriteLine ("Current Time:"+Now ); System.Threading.Thread.Sleep ( +);//program pauses for one secondnow = Now. AddSeconds (1);//time increases by one second            }            //0 O'Clock Midnight Thief arrives, watchdog raises alarm eventConsole.WriteLine ("the midnight of the Black Moon:"+Now ); Console.WriteLine ("The thief crept into the master's house ."); Dog.            Onalarm ();        Console.ReadLine (); }    }

Continue:--"

C # (event triggering) callback function, perfect handling all kinds of incurable diseases!

Related Article

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.