Introduction to Native EVENT

zhaozj2021-02-08  233

Almost unknown VC7, C provides Native Event this extension function.

Background and motivation: When an object changed, how did he notify "care" other objects? C has no good mechanism to solve this problem. The general method is to adopt a way to inherit the callback function from the C language. Definition method of function pointer int function (int, int, int code) (int, int, int); // fp is a function pointer, fp = func;

This callback mechanism has several problems 1. A callback function can only notify one accept object, which cannot notify multiple recipients 2, if not accepting objects need to be judged separately 3, using class functions to make a callback function will be more troublesome, generally used Static function

Boost :: Function is a package class for the callback function. How many problems have been alleviated above.

If you are Windows programming, you can use another mechanism to complete the "Notification" task between this object. For example, news, EventObject. But these ways depend on with complex SDK APIs. Not the function of the C language itself.

In short, C actually does not have a model of its own event.

Model: Starting from VC7, MS provides an extended C event model: Unified Event Model. This model is unified to provide a consistent way to solve the needs of event notification in Native C (non-COM), COM, and Managed Classes.

Applicable: This model supports event notification between objects. Support multi-line city concurrent. Support from Having Event Class Inherit, also supports the event definition in subclasses.

Example: An example of basic Native Event is provided on the MSDN. URL: http://msdn.microsoft.com/library/en-us/vccore/html/vcconeventlandlinginnativec.asp

Analysis: We look at the internal mechanism of Native Event through a small program. This applet and analysis conclusion comes from http://lamoo.s53.xrea.com/diary/diary2003a.html I have a small modification. (see attached)

1. An Event takes up 4BYTES's memory. 2. When the memory is allocated, the Event defined in a class will focus on the final centralized allocation of the class. 3. Maintain a one-way linked list in the order of the hook event. 4, the Acceptance of the hook event is removed if delete is deleted. If there is no clear unhook, the next RAISE event is an unpredictable error. 5, Event_Source and Event_Receiver attributes can be omitted.

Really: I use Native Event in one of my programs, which is a class that automatically downloads the update module over the HTTP protocol. That class instance is running in a separate line city, informing the connection network, downloading progress by Event, the Father City is a dialog, and the information received will be displayed. All this is similar to the download dialog of IE. In other words, the event is the collapse of the city notice. If you are familiar with the ATL7, there is a CatlhttpClient class in it, which is a callback function. The usual practice is often sent by SendMessage to a window handle of a parent gate, calls a function of display status through Windows message loop.

Question: We are now facing two questions: 1, a hook must correspond to a unhook, which is like allocating memory is a New must correspond to a delete. If you forget UNHOOK, a memory leak will occur. 2. Any recipient object is later downloaded later, it will not automatically unhook. So we have a subject, which is packaged in this event model to make a wrapper class to solve these two problems. This class should be similar to Auto_PTR, using the destructor automatic UnHook, but also support a wide variety of Event functions like Boost :: Function.

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

New Post(0)