Callback function theory

2010-08-10  来源:本站原创  分类:DotNet  人气:184 

Callback function theory

Statement CALLBACK

Calling (calling) mechanisms have been in the times from the compilation of extensive use of: preparing a ready-made code, the caller can always jump to this section of code, starting address, then return to jump when you perform a follow-up address. CPU prepared a ready-made call command, the call protection when stacking the scene, calling the pop-up after the site address from the stack to automatically return. Through the stack to protect the scene, it was transferred to the caller and can not know each other, then have a later function and components.

The call mechanism is not perfect. Callback function is an example. For example, write a quick sort function for others to call, which will contain relatively small. Trouble comes: At this point, and I do not know what to compare that kind of data - integers, floating point numbers, strings? So he had to for each type of data produced a different sort function. More common approach is to set out the parameters in the function of a callback function address, and inform the caller: Jun required to prepare a comparison function itself, which contains two pointer type parameters, the function pointer to compare the two referred to the size of the data, and Note by the function returns the value of the comparison. Sort function to call a function provided to compare the size of a pointer by passing parameters, you can totally regardless of the type of data being compared. Callee caller call back function (enough mouthpiece), the so called callback (callback).

Another Windows system also contains a wider range of callback mechanisms that the message mechanism. The basic message of this is the Windows control means, at first glance has nothing to do with the function call, is actually a disguised form of function calls. The purpose is to send a message to inform debit

Run a pre-prepared code, the equivalent of calling a function. News and LParam WParam attached to the equivalent of function parameters, but is more common than the average number of parameters. Application can take the initiative to send a message, more cases are waiting for Windows to send a message. Once the message into their message queues, they seized those interested, jump to the implementation of the appropriate message handling code. Operating system services for the application of this is, by the application to call. The application, once started, have to turn the call waiting for the operating system. This is obviously also a callback, or a generalized callback. In fact, the application can be formed between such a callback. If process A process B receive a message sent to start a piece of code, which Youxiang process A to send a message, which formed a callback. This callback is covert, Nongbu Hao will mess recursive call, if the lack of termination conditions will be endless loop until the process ruin. If deliberately written into the recursive call, and set conditions for a good end, is actually very interesting. However, the structure of this program is too hidden, unless it is necessary, or not at all.

Use of information can constitute a narrow callback. One example cited above sorting function, the callback function address can be replaced by the window handle. So, when the need to compare the data size, not to call the callback function, but by the SendMessage API function to send a message to the specified window. Comparison of the data received messages will be responsible for the size, the results of the comparison by the return value of the message itself passed the message sender. The realization of the function and the callback function is no different. Of course, this example is purely to draw feet on the snake news, sculptors the program was very slow. But other cases are not always the case, in particular the need asynchronous calls, sending messages is a good choice. If the callback function contains the file handle of the class of low-speed processing, such as the caller may not need to synchronize calls to the asynchronous call to start a separate thread, and then immediately implement follow-up code, the other to do things slowly to thread . An alternative approach is to use API function PostMessage to send an asynchronous message, and then immediately follow up the implementation of the code. This thread save trouble than many of their Gao Ge, and more secure.

Today we are living in a time object. As long as with the programming, whatever can not be separated object. But the object does not remove the callback, but it carried forward all over the place, but mostly in the incident (event) appeared as embedded in a fabric, seem more orthodox, more easily accepted. Application to use a component, first find out the total component properties, methods, and events, and then to the component property assignment, at the appropriate time call the appropriate method components, but also to write the event handling routines to prepare for component code to call. What event? It is merely a pointer to the address of the event routine, and a callback function addresses makes no difference. However, this callback method to be wise than many of the traditional callback function. First of all, it is to make people feel uncomfortable in the callback function into a natural handling routines, so that programmers Dunjue smoother. Furthermore, the address is a dangerous thing, make good use of the accelerated procedure, a trap with bad everywhere, the program to crash. Always thinking of modern programmatically hide the address (such as VB hides a more thorough and Java), the cost of the program is to reduce efficiency. Event routine to programmers without direct operation of address, but does not make programs slow down.

Custom callback function

Callback function is not explicitly call the function; by the address of the callback function passed to the caller in order to achieve call. Callback function to use is necessary, we want a unified interface different content, then go back out function is very appropriate. For example, several different devices we were writing a different display function: void TVshow (); void ComputerShow (); void NoteBookShow ()... and so on. This is what we want to use a single display function, then we can go back out the function of the.

void show (void (* ptr )());

When used according to different parameters of incoming call different callback function.

Different programming languages may have different syntax, the following give a callback function in c language example, one of the callback function with no arguments, another callback function arguments.

Example 1:

/ / Test.c

# Include <stdlib.h>

# Include <stdio.h>

int Test1 ()


int i;

for (i = 0; i <30; i + +)


printf ("The% d th charactor is:% c \ n", i, (char) ('a' + i% 26));


return 0;


int Test2 (int num)


int i;

for (i = 0; i <num; i + +)


printf ("The% d th charactor is:% c \ n", i, (char) ('a' + i% 26));


return 0;


void Caller1 (void (* ptr )())// pointer to a function for function parameters


(* Ptr) ();


void Caller2 (int n, int (* ptr )())// pointer to a function for function parameters, where the first parameter is a pointer to a function of services,

(/ / Can not write void Caller2 (int (* ptr) (int n)), so the definition of syntax errors.

(* Ptr) (n);



int main ()


printf ("************************ \ n ");

Caller1 (Test1); / / equivalent to calling Test2 ();

printf ("&&&&&&************************ \ n ");

Caller2 (30, Test2); / / equivalent to calling Test2 (30);

return 0;


Above address by the callback function passed to the caller in order to achieve call, but should be noted that the use of a callback function with parameter. To achieve the callback, you must first define the function pointer. Function pointers defined here a little mention. For example: int (* ptr) (); where ptr is a function pointer, which (* ptr) in the brackets can not be omitted, because parentheses take precedence over star, as has become a return type for the integer function declaration the.


Reprinted Address:

Note: The fox that the callback function with multi functions of the state function is most important is to provide a dynamic embed code means, to achieve a way to function within the disruption.

This article comes from CSDN blog, reproduced, please indicate the source:

  • Callback function theory 2010-08-10

    Callback function theory Statement CALLBACK Calling (calling) mechanisms have been in the times from the compilation of extensive use of: preparing a ready-made code, the caller can always jump to this section of code, starting address, then return t

  • Deal with the use of DWR callback function return a collection of the list and fill selec 2009-04-17

    1, configuration dwr.xml <? xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE dwr PUBLIC "- / / GetAhead Limited / / DTD Direct Web Remoting 1.0 / / EN" ""> <

  • dwr how to obtain the return value from the callback function to the scope of 2009-11-07

    Dwr when in use, in accordance with access to a pojo object id, js on the page to be a js object to be converted, it is easy to use. However, the return value can only be used inside dwr callback function, and then the return value of this assignment

  • Struts2 + jQuery to achieve ajax (2) callback function 2010-03-18

    Scenario Description: Ajax using Struts2 + jQuery to achieve the time, how to obtain the return value of the information to correctly prompt the user. For example, to scoring a movie rating, each user can only play once a film score, if the play when

  • HibernateTemplate template model and the callback function 2010-03-31

    spring in the HibernateTemplate offers Hibernate DAO mode support, through the configuration, it is easy to be able to use some common features. Here personally feel good template model is used, there is a callback function to use. Excellent framewor

  • ajax callback function parameter passing 2010-03-30

    [Reproduced in] Keywords: Function parameter passing attribute approach can take parameters: Js Code function ClassX(name) { = name; = function (param) { alert( + "

  • BREW in the callback function on a number of brief 2010-02-26

    Introduced to the c (Brew Platform) language CallBack (callback function) mechanism to cite examples of Brew's IMedia are explained in detail as follows: User program (user process is running) need to use the Brew system services (system process to r

  • ajax callback function parameter 2010-03-30

    [Reproduced in] Keywords: Function parameter property is available parameters: Js code function ClassX(name) { = name; = function (param) { alert( + " " + para

  • understanding javascript callback function 2010-04-07

    javascript callback function to understand: Callback function is actually just an ordinary function, this "correction" is generally appear in the AJAX application called, is the AJAX function to return the results. Callback function is a functio

  • Callback function to the variable assignment 2010-04-19

    DWR practical technology: 1, the callback function assigned to the variable function removeByAppId (leftValue) ( var strSource = flowfrm.iamFlowsScope.value; DWREngine.setAsync (false); flowDefAJAX.removeByAppId (leftValue, strSource, callBackFun); /

  • c # delegate is equivalent to c + + the callback function (function pointers) 2010-05-12

    Callback function, which is to write your own. You need to call another function and this function is one of the parameters, this is your callback function name. Thus, the system when necessary, will call the callback function you write, so you can f

  • jquery plug-in callback function 2010-05-26

    Today someone said to me that he wanted to plug in my implementation of a method call is completed, that I do not have universal plug-ins, today came back and looked, looked good under the js callback function, the decision to add back in the plug-in

  • Understanding of Java callback function 2010-07-13

    The so-called callback, that is, client service program C calls a function S in A, then S has at some point turn, a function called C in B, for C, this B will be called the callback function. For example, under Win32 window procedure function is a ty

  • PHP callback function to use 2010-07-16

    Callback function is actually a function call (usually the API functions), it will be a function of their own (this function is a callback function) of the address as a parameter to the function. Callbacks and API is very close, they have in common i

  • Point on the record callback function Ajax 2010-09-13

    var flag = false; $.getJSON(url, params, function(data) { // jQuery The default use of AJAX is asynchronous implementation // So the statement [[[2]]] The vast number of times over here to do, here is the equivalent of using a new thread to execute t

  • A preliminary understanding of java callback function 2010-09-16

    Callback function, primarily to handle asynchronous callback interface to be achieved through the implementation of the order: 1. Test call testtime 2. Testtime function implementation of the Interface 3. Back to the test class implementation of the

  • as in the callback function 2010-10-16

    / ** * Callback function example * 2010-8-26 9:52 * @ Author zking * / package { import flash.display.MovieClip; import DemoClass; public class Main extends MovieClip { protected var demo: DemoClass = new DemoClass (); public function Main () { demo.

  • FLex written callback function 2010-11-28

    I now have a technology needs are: A page pops up a form B, in B after the operation and return values passed back to the A page, A page after receiving the return value for the appropriate treatment. I have taken the approach: In B, made the followi

  • ajax to pass parameters to the callback function onreadystatechange 2010-12-16

    xmlHttp.onreadystatechange = function () {callback (a, b);}; which to pass the a and b are two parameters. Then I discovered a method of their own, the method certainly a lot of people know, but I did not search on baidu that written here, to promote

  • GTK callback function and a multi-parameter passed on the details of memory allocation 2010-12-26

    Bind a GTK callback function to the corresponding elements in the basically provided by GObject g_signal_connect () and other functions, and the definition of the form of a callback function is void function (GtkWidget * widget, gpointer data); It is