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!