Callback (computer programming)

A callback function (English callbackFunction ) refers to computer science, a function that is another function passed as a parameter and called by this under certain conditions. This approach follows the design pattern of Inversion of Control.

Callback in the same sequence

The picture shows a call to a routine in another context ( operating system call, another software module). This call is passed a reference to the callback routine. The called routine can then call the callback routine in their sequence. The callback routine ultimately stores data that can be used in the further execution upon return of the original call. In the English literature, this form is called a blocking or synchronous callback callback.

The advantage of this technique is that the access is formulated on the data in its own context and / or in the recall can be any programmatic preparation: The parameters and the call time to be determined in another software module, the determination is what carried out in the recall is for the own software module.

This makes it possible to define functions and generally only be determined when the function by specifying the callback function the behavior accurately. Frequently gets a callback function does not have a name, but is an anonymous function defined directly in the call (see also lambda calculus ). About callback functions to reach a loose coupling between individual components.

Recall from another thread

If you specify a callback function it is up to the called function when it executes the callback ( Inversion of Control ). It is possible that the recall is done at a later date from another thread out. The picture shows the principle. The assignment of the callback routine to its own context is the same as in the above scheme. Since the recall can be done in another thread, but the conditions for data consistency are also observed ( mutex ). In the English literature, this form is called a deferred callback or asynchronous callback.

An example of this is the call to SetTimer ( hWnd, ident, milliseconds, callback routine ) in the Windows API. The call SetTimer ( ... ) is a short operating system call initializes the timer only in the operating system. The callback routine is only called when the time has expired. This happens regardless of the sequence in a thread of the operating system after the call to SetTimer (...). In the callback routine to be provided can be run arbitrary code. However, it is to note that the callback routine may only be short and non-blocking. Otherwise there is a risk that the entire operating system, or at least the current process is blocked. Ultimately, the callback routine should really take only data and not involve complex actions themselves. In particular, can be created and stored for an event-oriented programming, an event instance that controls other processes.

Durable callback routine

A callback routine can only apply each of an invocation. The callback routine can be called several times in reference to the call to the associated action is completed, so temporary.

But the permanent assignment of a routine is sometimes called callback routine, see for example msdn.microsoft ... ThreadProc callbackFunction: When creating a thread using the Windows API, the thread function is specified as a callback function.

The assignment of an event handler, for example, to an HTML element

159307
de