How to: subscribe to and unsubscribe to events (C # programming guide)

Source: Internet
Author: User

C # programming guide

How to: subscribe to and unsubscribe to events (C # programming guide)

If you want to write custom code called when an event is triggered, you can subscribe to events published by other classes. For example, you can subscribe to the "click" event of a button so that the application can perform some useful operations when you click the button. Use Visual Studio 2005 ide to subscribe to events. If the "properties" window is invisible, right-click the form or control to create an event handler in the "design" view, then select "attribute ". At the top of the Properties window, click the event icon. Double-click the event to be created, for example LoadEvent. Visual C # creates an empty event handler method and adds it to your code. Alternatively, you can manually add code in the "code" view. For example, the following code line declares FormClass LoadThe method of the event handler called during the event. C # copy the code privatevoid form=load (Object sender, system. eventargs e) {// Add your form load event handling code here .} the code lines required to subscribe to this event will also be in form1.designer of your project. CS File InitializecomponentMethod. The code line is similar to: copy the code this. load + = new system. eventhandler (this. formateload); subscribes to an event programmatically and defines an event handler method. Its signature matches the delegate signature of the event. For example, if an event is based on the eventhandler delegate type, the following code indicates the method stub: copy the code void handlecustomevent (Object sender, customeventargs A) {// do something useful here .} use the addition assignment operator (+ =) to attach an event handler to the event. In the following example, assume that PublisherObject has Raisecustomevent. Note that the subscription class must reference the issuer class to subscribe to its events. Copy the code publisher. raisecustomevent + = handlecustomevent. Note that the above syntax is the new syntax in C #2.0. It is equivalent to the C #1.0 syntax and must use the following new keywords to explicitly create an encapsulation delegate: copy the code publisher. raisecustomevent + = new customeventhandler (handlecustomevent); Use the anonymous method to subscribe to an event and use the addition assignment operator (+ =) to append an anonymous method to the event. In the following example, assume that PublisherObject has RaisecustomeventAnd defines CustomeventargsClass to carry some types of special event information. Note that the subscription class must be referenced PublisherTo subscribe to events. Copy the code publisher. raisecustomevent + = delegate (Object o, customeventargs e) {string S = O. tostring () + "" + E. tostring (); console. writeline (s) ;}; note that If you subscribe to an event using an anonymous method, canceling the subscription process is troublesome. To cancel the subscription, return to the subscription code of the event, store the anonymous method in the delegate variable, and add the delegate to the event. To avoid calling the event handler when an event is triggered, you only need to cancel the subscription. To prevent resource leakage, cancel the subscription before releasing the subscription object. This is important. 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 contains this reference, no garbage collection is performed on the subscription object. To cancel a subscription event, use the minus value assignment operator (-=) to cancel the subscription event: copy the code publisher. raisecustomevent-= handlecustomevent; after all subscribers cancel an event subscription, the event instance in the issuer class is set to null. (Source: msdn)

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.