Callback

xiaoxiao2021-03-06  45

The reason why "First, the first briquette, the first is deep", but it is difficult to know, so it will often give the technical small details, it is dizzy, among which "callback function" is one of them, although returned Use, but always don't understand the meaning of it, and many books have also been concealed. It is not until recently, it is more clearly understood. Summary, the callback mechanism includes two parts: service executors and service formulars. 1. Service executors set a service specification first; 2. Serve the service formulars and then set the service mode according to the specification; 3. The executor provides services in this way. The callback function is to pass the function pointer as a parameter, so the specification is the parameter type, number of the agreement function. This article is as follows: Calling mechanism has been used in a lot of use from the assembly era: preparing an outgoing code, the caller can jump to the start address of this code, and then return the subsequent address when the jump is returned. The CPU prepares ready-made call instructions for this purpose. When calling the site, the field address is played from the stack after the call is completed to automatically return. It is a wonderful invention of the stack protection, which makes the caller and the adjusters without each other, so there is a later function and components, so that the program is so happy. If the selection is the most influential inventions of the human beings, after the fire and the wheel, the author is pushing the stack call. Although this is said that this call mechanism is not perfect. The callback function is an example. The function and other meals prepared for the caller, and its cooking deals with diners, but the truth is not the case. For example, write a quick sort function for others to call, which must contain a relatively small. Trouble coming: What data-integer, floating point, string? Thus, there is only a different sort function for each type of data. More password is to column a callback function address in the function parameters, and notify the caller: Jun must prepare a comparison function, which contain two pointer parameters, the function should compare the size of this two pointer index, and by Function Return Value Description Comparison Results. The sort function is relatively small, and the parameters are passed by the pointer, and the comparative data type can be fully compared. The caller is turned back to call the caller's function (sufficient bite), so it is called a callback. The callback function makes the program structure a lot. There are many callback functions in the Windows API function. Although there is a detailed description, the beginners still make the beginners fog. I am afraid this is also helpless. No matter what kind of things, it can be comfortable with the tree structure single-way description. If a grandchildren in a family is a ancest, I am afraid that no one can clarify the clue. But the complex data processing often needs to form a mesh structure, and non-simple client / server relationships can be exhausted. The Windows system also contains another more wide callback mechanism, namely a message mechanism. The message is the basic control means of Windows, and the watch is not related to the function call, it is actually a variable function call. The purpose of sending messages is to inform the recruitment to run a preparatory code, which is equivalent to calling a function. The WPARAM and LPARAM included with the message are equivalent to the parameters of the function, but more common than normal parameters. The application can actively send messages, more cases, waiting for Windows send messages. Once the message enters the message queue, those who are interested, jump to perform the corresponding message processing code. The operating system is called the application service, called by the application. Once the application is started, it will be in turn to wait for the call to the operating system. This is also a callback, or is a generalized callback.

In fact, this callback can also be formed between the applications. If the process B receives the message sent by the process A, it starts a piece of code, which sends a message to the process A, which forms a callback. This kind of callback is more concealed, and it will not be able to recurrently call. If you deliberately write this recursive call, it is very interesting to terminate the conditions. But this program structure is too hidden unless it is necessary, it is still not necessary. Using messages can also be constituted a narrow callback. An example of the above-mentioned sorting function can be replaced with the callback function address to the window handle. Thus, when comparing data is required, the callback function is not called, but transmits a message to the specified window by using the API function sendMessage. Receiving the message party is responsible for comparing the data size, transmitting the comparison result to the message sender through the return value of the message itself. The functionality implemented is not different from the callback function. Of course, this example is changed to the news purely seduce snake to fate, but the program is very slow. But in other cases, it is not always, especially when you need asynchronous calls, the send message is a nice choice. If the callback function contains low-speed processing, the caller, etc., the caller is not allowed to change the synchronization call to asynchronous calls, start a separate thread, then immediately perform subsequent code, and the rest will make the thread slowly do it. An alternative is to send an asynchronous message by the API function PostMessage, and then perform subsequent code immediately. This is much better than yourself, and safer. Now we are living in an Object era. As long as you are related to programming, no matter what happens, you can ink Object. However, Object did not eliminate callbacks, but in turn, it has all, it is, but most of them appear in an event (Event), inlaid in a certain structure, which is more intended to be accepted. The application uses a component, always draw the properties, methods, and events of the component, and then assign the component attribute, call the appropriate component method when appropriate, and write the handup routine for the event to prepare component code. Call. What is the event? It is just a address that points to the event routine, and there is no difference with the callback function address. However, this callback is much better than traditional callback functions. First, it turns a very uncomfortable callback function into a natural handling routine that makes the programmer feel smooth. Furthermore, the address is a dangerous thing, using it to accelerate the procedure, use a nothing to be a trap, and the program will crash at any time. Modern programming methods always think about hide the address (hidden than VB and Java), which costs the program efficiency. The event routine allows the programmer without direct operating addresses, but does not decelerate the program. What is even better, this change, this is the strange skill of the damage program to become a new design concept. Not only is it to be criticized, but also forced to wait for the mortal clean hand, and study, admire . Just accidentally thinking about it, I couldn't find a bottle of old wine, so I caught this anecdote, let Zhu Jun smiled. The event driver design is formed around the message base, an event occurs, accompanied by a lot of news. I understand that the "callback mechanism" is WINDOW to call a specified function during the execution of an API function.

We can simulate: assuming that MS provides enumfont, which is all fonts, assuming that its implementation is enumfont () {while ((f = findnextFont ())! = Null) {printf ("fontname:" f.name);}} This is displayed in the loop display all font names. However, developers may use another place to font, so how can developers can use this information, so improved: enumfont (void * userfunc) {while (f = findnextfont ())! = Null) {printf ("FontName:" f.name); if (userfunc! = null) userfunc (f);}} Hypothesis UserFunc is a function Void F (FontObject Font). This user only needs to define a function: void myfunc (fontobject Font) {listCtrl.addstring (font.name);} You can add all font information to a list box by using EnumFont (MyFunc). Then we call myFunc is a callback function that makes a function called by a system function. It can therefore be concluded that the 1 callback function is a function 2 callback function defined by the developer in accordance with a certain prototype and does not cause the developer to directly call the execution of the 3 callback function. It is usually transmitted to the system API as a parameter, which is called by the API. 4 The callback function may be called once by the system API, or it may be called multiple times. For example the function int EnumFontFamilies (HDC hdc, // handle to device control LPCTSTR lpszFamily, // pointer to family-name string FONTENUMPROC lpEnumFontFamProc, // pointer to callback function LPARAM lParam // pointer to application-supplied data); wherein the FONTENUMPROC lpEnumFontFamProc It is a callback function that is defined in accordance with the format INUMLOGFONTFON ENUMFONTFAMPROC (Enumlogfont Far * LPNTM, INT FONTTYPE, LPARAM LPARAM).

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

New Post(0)