".NET Framework Design" Chapter 11 Event

xiaoxiao2021-03-06  84

Chapter 11 Events

Summary:

This chapter describes the application of events, including:

N release event design mode

n Listening event

N Explicit Control Event Registration

n A type defines multiple events and reducing memory resources

First, release an 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 Define an event additional information type

l Delegate type (DELEGATE callback function) when defining an event trigger

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 convert input into events

3, some agreements in the .NET framework:

The L.NET framework suggests that the additional information type name ends with Eventargs ([EventName] Eventargs; no need to pass additional information, Eventargs.empty Static Read-only field

L.NET framework suggestion entrustment is: void [Eventname] EventHandler (Object sender, [Eventname] EventArgs E); uses the SYSTEM.EVENTHANDLER delegate type without additional information, and uses Eventargs.emtpy static read-only fields as the second parameter

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] // For thread security, you need to overhead

Public void add_eventmsg (eventnameeeventhandler handler)

{

EventMSG = (eventnameeeventhandler) delegate.combine (eventmsg, handler);

}

[Methodimplattribute (MeghodImploptions.Synchronized] // 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, listening events

The listening process is divided into the following steps:

l Define the event to notify the callback function, which is processed in it.

l Registering 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 the original delegate 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 exquisite definition, the method is also a method of adding an event to the collection.

l Establish a unique key for each event (identifying a static read-only object) to identify events

l Publishing of the rest of the single event

V.

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:

ü Debul's application (detailed in Chapter 17)

ü Thread safety guarantee

Ü Hash table (Hashtable)

There will be detailed in subsequent notes.

转载请注明原文地址:https://www.9cbs.com/read-122399.html

New Post(0)