?????? This chapter describes the application of the event, including:
N???????? Publish Event Design pattern
N???????? Ways to listen for events
N???????? Explicit control of Event registration
N???????? A type defines multiple events and reduces memory resources
?
One???????????? Publishing events
1, the types of publishing events provide the functionality:
L???????? Allow other objects to register events
L???????? Allow other objects to unregister events
L???????? Maintains a list of registered objects and notifies the appropriate registration object when the event occurs
2. Post Event steps:
L???????? Defining event-Attached information types
L???????? Defines the delegate type that is invoked when an event is triggered (delegate callback function)
L???????? Define event members. Shaped like: public event [Eventname]eventhandler MSG;
L???????? Defines a protected virtual method (protected virtual), which is responsible for notifying the registered object of the event
L???????? Defines a method that converts an input into an event
3,. NET Framework's conventions:
L???????? . NET Framework recommends additional information type names end With EventArgs ([Eventname]eventargs); use eventargs.empty static read-only field for events that do not need to pass additional information
L???????? . NET Framework recommends that the prototype be: void [Eventname]eventhandler (Object sender, [Eventname]eventargs e); Direct use of System.EventHandler delegate type without additional information, and use eventargs.emtpy static read-only field as 2nd argument
4, analysis of the definition of the event:
The following event definitions are available in the type of the publication event:
?
public event Eventnameeventhandler Eventmsg;
?
is translated by the compiler:
?
Private Eventnameeventhandler eventmsg = null;
[MethodImplAttribute (meghodimploptions.synchronized)]?????? For thread safety, cost is required
public void Add_eventmsg (Eventnameeventhandler handler)
There is a similar way of contacting explicit definition of an event.
Two???????????? Listening for events
The listening process is divided into the following steps:
L???????? Defines an event notification callback function in which to handle the reached event
L???????? Register this object to the type of the publication event
L???????? Unregister this object
Three???????????? Explicit control of Event registration
Display control event Registration is intended to eliminate the overhead of thread synchronization in single-threaded applications (see the profiling of event definitions above), so it is only useful to display publishing events when thread synchronization is not required.
Refer to the description in the publishing event, which explicitly controls event registration by breaking down the definition event member into:
L???????? Defining delegate Types
L???????? Explicitly defines an event and accessor method. Note the Add and remove methods must be defined at the same time
L???????? Modify the method of notification event enlistment object, using the delegate type you just defined
Four???????????? Defining Multiple Events
Defining multiple events is primarily to expose a large number of events but not assign fields to each event, and use a hash table or a list, such as the System.ComponentModel.EventHandlerList in FCL, to store these event instances. This is only useful if there is a very large number of events and it is expected that most of the events will not be implemented.
?
To understand this part of the content to grasp the following aspects:
L???????? An analysis of the event definitions above, and the method of adding an event delegate to the collection
L???????? Establish a unique key for each event (identified by establishing a static read-only object) to identify the event
L???????? Other releases of the same single event
Five???????????? Example
See another article, "chapter 11th multi-event example [Story of a man and three women]"
?
Other points of knowledge that are involved in this chapter:
U???????? Application of the Commission (detailed in the 17th chapter)
U???????? Thread-Safe protection
U???????? The application of hash list (Hashtable)
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.