C # Event Basics

Source: Internet
Author: User

What is the event?

An event represents a signal that a significant thing has happened in the program. It includes the sender and receiver of the event. We call the object that triggers the event the sender of the event, and the object that responds to the event is called the receiver of the event . In C #, events and delegates are often linked together, and their relationships are those of objects and classes.

In the form application development of C #, Many delegate types that are specifically used for events are predefined in. NET, such as EventHandler, keyeventhandler, MouseEventHandler and so on. Here therefore the main introduction is how to customize an event.

How to use Events

(1) The use of events is actually a process of creating an event driver. The following steps are generally taken:

     1      2      3      4      5      6

    (2)   write a custom event.

There is a rabbit and a sheep in the deep forest, when the rabbit sleeps at night, the sheep is responsible for guarding the hole. If there are wolves, the sheep issued a Alarm incident, the Rabbit received Alarm incident, and the sheep to take action. Suppose the Wolf arrives at 12 o'clock on the August 3 night of the year.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceEventdemo8 {9     classSheepTen     {  One         //1. Declaration of the Commission on the event; A          Public Delegate voidAlarmEventHandler (Objectsender, EventArgs e); -          -         //2. Declare the event using the event keyword; the          Public EventAlarmEventHandler Alarm;  -          -          Public voidOnalarm ()//3. Write the function that raises the event; -         { +             if( This. Alarm! =NULL) -                  This. Alarm ( This,NewEventArgs ());//issue an alert +         } A     } at         //Event Recipients -     classRabbit -     { -         //4. Writing Event handlers -         voidRabbithandlealarm (Objectsender, EventArgs e) -         { inConsole.WriteLine ("Rabbit: The Wolf is coming, hide now! "); -         } to         //5. Registering event handlers; +          PublicRabbit (Sheep Sheep) -         { theSheep. Alarm + =NewSheep.alarmeventhandler (rabbithandlealarm); *         } $     }Panax Notoginseng     //6. Now to trigger the event -     class Program the     { +         Static voidMain (string[] args) A         { theSheep Sheep =NewSheep (); +Rabbit Rabbit =NewRabbit (sheep); -DateTime now =NewDateTime ( .,8,2, at, -, -); $DateTime midnight =NewDateTime ( .,8,3,0,0,0); $Console.WriteLine ("time passes by a second, waiting for midnight to arrive ..."); -              while(true) -             { theConsole.WriteLine ("Current Time:"+Now ); -                 //0 o'clock Midnight, wolves haunt, sheep cause Alarm events.Wuyi                 if(now = =midnight) the                 { -Console.WriteLine ("The sand rustling, the wolf quietly came to the sheep near ..."); Wu                     //Raising an event -Console.WriteLine ("\ n Sheep alarm: Wolf infestation, Baa ~~~~~~~"); About sheep. Onalarm (); $                      Break; -                 } -System.Threading.Thread.Sleep ( +);//program pauses for one second -now = Now. AddSeconds (1);//time increases by one second A             } +         } the     } -}

When the thief arrived at midnight, sheep  call sheep. Onalarm () alarm event, The system then found and executed the event handlers registered in  alarm rabbithandlealarm () < Span class= "FONTSTYLE0" >
onalarm () alarm () alarm  link up.  

Precautions

the parameters of the event handler should be the same as the event delegate. In general, an event handler accepts two parameters,

One is the sender of the event Sender, and one is the event argument E.

Ending .....

C # Event Basics

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.