Chapter 11 Events
Summary:
?????? This chapter tells the application of the event, including:
n ???????? Post event design mode
N ???????? Method for listening
N ???????? Explicit control event registration
n ???????? A type define multiple events and reduce memory resources
?
First, ???????????? release event
1. Publish the function of the type of event:
l ???????? Allow other object registration events
L ???????? Allow other objects to log out events
l ???????? Maintain a list of registration objects, notify the corresponding registration object when the event occurs
2. Publish an event step:
l ???????? Defining an event additional information type
l ???????? Delegation type (DELEGATE callback function)
l ???????? Define event members. Shape: public event [Eventname] EventHandler MSG;
l ???????? Define a protected virtual method, responsible for notifying the registration object
l ???????? Define a way to transform into an event
3, some agreements in the .NET framework:
l ???????? .NET framework suggest that the additional information type name is ended in Eventargs ([EventName] Eventargs; no additional information is required to use Eventargs.empty Static Read-only field
l ???????? .NET framework suggestion entrusting prototype: void [Eventname] EventHandler (Object sender, [Eventname] EventArgs E); use Eventargs.emtpy without additional information Static read-only field as the second parameters
4. Analysis of the definition of the event:
If you have the following event definitions in the type of event:
?
Public Event EventnameEventHandler EventMsg;
?
The compiler is translated to:
?
Private eventnameEventhandler eventmsg = null;
[METHODIMPLATTRIBUTE (MeghodImploptions.synchronized)] ?????? // Used for thread security, you need to overhead
Public void add_eventmsg (eventnameeeventhandler handler)
{
?????? Eventmsg = (eventnameeeventhandler) delegate.comBBine (EventMSG, HANDLER);
}
[METHODIMPLATTRIBUTE (MeghodImploptions.synchronized)] ?????? // Used for thread security, you need to overhead
Public void transove_eventmsg (eventnameeeventhandler handler)
{
????? Eventmsg = (eventnameeeventhandler) delegate.remove (eventmsg, handler);
}
?
Contact the explicit definition event method, there is a wonderful work.
Second, ??????????? a listening event
The listening process is divided into the following steps:
l ???????? Define the event to notify the callback function, in which the incident is processed
l ???????? Register this object to the type of publishing event
L ???????? Log out of this object
Third, ???????????? Explicit control event registration
Display Control Event Registration is to eliminate overhead due to threaded applications in a single-threaded application (see analysis of event definitions above), so it is only useful when there is no need to synchronize. The description in the reference issuance event, explicit control event registration is just the decomposition of "defined event members":
l ???????? Define the principal type
l ???????? Explicitly defined events and accessor methods. Note that the add and remove methods must be defined at the same time.
l ???????? Modify the method of notifying the event registration object, using a just defined entrustment type
Fourth, ???????????? Define multiple events
Define multiple events Mainly to store these event instances in order to open a large number of events but do not assign a field for each event, use a hash table or linked list (such as system.componentmodel.eventhandlerList in the FCL). This is only useful when there are very many events and most events expect.
?
Understand this part of the content should seize the following aspects:
L ???????? The above analysis of the event definition, wherein the method is also a method of adding an event commissioned in a collection.
l ???????? Establish a unique key for each event (identifying a static read-only object) to identify the event
l ???????? the release of the rest of the same event
5, ????????????
Please refer to another article "The eleventh chapter multi-event example [a man and the story of three women]"
?
Other knowledge points involved in this chapter:
ü ???????? Application (Chapter 17)
ü ???????? Thread security
ü ???????? has a HashTable application
There will be detailed in subsequent notes.
?