The following uses the webbrowser. documentcompleted event as an example to describe how to subscribe to an event using C.

Source: Internet
Author: User

1. subscribe to and process events

(1) define an event processingProgramMethod. The signature must match the delegate signature of the event. For example, if the event is based on the webbrowserdocumentcompletedeventhandler delegate typeCodeThe corresponding event processing function is defined:

Void handledocumentcompletedevent (Object sender, webbrowserdocumentcompletedeventargs e) <br/>{< br/> // do something useful here. <br/>}

Handles the webbrowser. documentcompleted event and receives notifications when the new document is loaded. The webbrowserdocumentcompletedeventargs object passed to the event handler enables you to retrieve the location of the new document through URL attributes. If the webbrowser. documentcompleted event occurs, the new document is fully loaded, which means you can access the content of the document through the webbrowser. Document, webbrowser. documenttext, or webbrowser. documentstream attribute.

(2) Use the addition assignment operator (+ =) to attach an event handler to the event.

Webbrowser1. documentcompleted + = handledocumentcompletedevent;
Note that the above example uses the new syntax in C #2.0. This syntax is equivalent to the C #1.0 syntax that must use the new keyword to explicitly create an encapsulation delegate:

Webbrowser1.documentcompleted + = new webbrowserdocumentcompletedeventhandler (handledocumentcompletedevent );

2. cancel subscription events

To prevent calling the event handler when an event is triggered, cancel the subscription. To prevent resource leaks, you should cancel the subscription before releasing the subscribers. Before canceling an event subscription, the multicast delegate that serves as the basis of the event in the publish object references the delegate that encapsulates the event handler of the subscription. As long as the published object remains referenced, the garbage collection function will not delete the consumer object.

Use the minus value assignment operator (-=) to cancel the subscription event:

Webbrowser1.documentcompleted-= handledocumentcompletedevent; // C #2.0 syntax </P> <p> webbrowser1.documentcompleted-= new syntax (handledocumentcompletedevent); // C #1.0 syntax <br/>

3. Anonymously subscribe to events

If you do not need to unsubscribe to an event in the future, you can use the addition assignment operator (+ =) to append the anonymous method to this event. In the following example, assume that the object named publisher has an event named raisecustomevent and defines a customeventargs class to hold some types of special event information.

 
Publisher. raisecustomevent + = delegate (Object o, customeventargs e) <br/>{< br/> string S = O. tostring () + "" + E. tostring (); <br/> console. writeline (s); <br/> }; 
Note: If you use an anonymous function to subscribe to events, the cancellation of the event subscription process will be troublesome. In this case, to cancel the subscription, you must return the subscription code of the event, store the anonymous method in the delegate variable, and then add the delegate to the event. Generally, if you have to unsubscribe to an event in the code that follows, we recommend that you do not subscribe to this event using an anonymous function.

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.