The eventhandler proxy is used to process an event without event data.
Whenever an event occurs, the event agent is called to trigger other events driven by the previous event (Listen to the current event tcurrentevent + = tlistenerevent ).
Public Delegate void eventhandler (Object sender, // event initiator eventargs E // object for Processing Event Data)
The eventargs class is the base class of the event variable and provides the event data type to an event.
System. Object
System. eventargs
More...
public class EventArgs
The following example shows an event named thresholdreached that is associated with an eventhandler delegate. The method assigned to the eventhandler delegate is called in the onthresholdreached method.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace delegrate {class program {static void main (string [] ARGs) {Counter C = new counter (new random (). next (10); C. thresholdreached + = c_thresholdreached; // declare an event listener console. writeline ("Press 'A' key to increase total"); While (console. readkey (true ). keychar = 'A') {console. writeline ("Adding one"); C. add (1) ;}} static void c_thresholdreached (Object sender, thresholdreachedeventargs e) {console. writeline ("the threshold of {0} was reached at {1 }. ", E. threshold, E. timereached); environment. exit (0) ;}} class counter {private int threshold; private int total; public counter (INT passedthreshold) {Threshold = passedthreshold;} public void add (int x) {total + = x; If (total> = Threshold) {thresholdreachedeventargs ARGs = new thresholdreachedeventargs (); args. threshold = threshold; args. timereached = datetime. now; onthresholdreached (ARGs); // transmit a teventargs --> entity} protected virtual void onthresholdreached (entity e) {eventhandler <entity> handler = thresholdreached; // handle an event if (handler! = NULL) {handler (this, e); // trigger c_thresholdreached} public event eventhandler <strong> thresholdreached;} public class handle: eventargs {public int threshold {Get; set;} public datetime timereached {Get; Set ;}}}