an event is a method that allows sending signals to indicate the occurrence of an important event.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace event {class program {static void main (string [] ARGs) {longtask it = new longtask (); it. onpolicyprogress + = new longtask. notifyprogressdelegate (it_onnotifyprogress); it. performtask (); console. readline ();} static void it_onpolicyprogress (progressargs Pa) {console. writeline ("Progress on long task complete. {0} % complete. ", Pa. percentcomplete) ;}} class longtask {Public Delegate void policyprogressdelegate (progressargs Pa); // declare a delegate public event policyprogressdelegate onpolicyprogress; // create an event public void limit mtask () {for (INT I = 0; I <10000; I ++) {// perform some processing //... // configure y subscribers that progress was made if (I % 100 = 0) {onnotifyprogress (New progressargs (INT) I/100 ));}}}} class progressargs {public int percentcomplete; Public progressargs (INT pctcomplete) {percentcomplete = pctcomplete ;}}}Event Overview
Events have the following features:
The publisher determines when an event is triggered, and the user determines the operation to be performed to respond to the event.
An event can have multiple subscribers. One subscriber can process multiple events from multiple publishers.
Events without subscribers will never be called.
Events are usually usedNotificationUser operations (for example, click a button in the graphic user interface or select an operation from the menu ).
If an event has multiple subscribers, when this event is triggered, multiple events are synchronously called for processing.Program. To call events asynchronously, see call synchronous methods in asynchronous mode.
You can use the event synchronization thread.
In the. NET Framework class library, events are based on the eventhandler delegate and eventargs base class.