Visual Studio eventhandler delegate and eventargs

Source: Internet
Author: User

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 ;}}}

  

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.