callback function:
A callback function is a mechanism in which the caller passes some arguments to an object when it initializes an object (where the object is generic, including objects in OOP, global functions, and so on), and passes the address of a function that a caller can access to the object. This function is a notification convention between the caller and the callee, and when the specified event occurs, the callee (typically including a worker thread) calls the function according to the callback function address.
In this way, the caller is in a thread that is invoked by another thread.
In the Windows API, there are functions that use callback functions, such as CreateThread, SetWindowLong, and so on. The corresponding callback function is defined as the following form:
function Callbackfunc (Wnd:hwnd; MSG, WParam, lparam:longint): Longint;stdcall;
Procedure Threadfunction (ptr:pointer); stdcall;
News:
A message can also be seen as some form of callback, because the message is also passed by the caller to the callee and a message number at the time of initialization, and the caller sends a message to the caller when the agreed event occurs.
In this way, the caller is in the main thread, and the callee is in the main thread or worker threads.
Delphi Event Model:
There are a number of visual components in Delphi's VCL that use event models, such as Tform oncreate events, that specify event functions at design time, and at Run-time events, call event functions that are specified at design time.
On the mechanism, the Delphi event model is the same as the callback. But the concrete form is somewhat different, the pure callback function is the form of the global function, and the Delphi event is the form of the object method, that is, the following callback function type can be defined
Type
Tcallbackfunc = procedure (pdata:pointer) of object;
This makes the Delphi event can only be used inside Delphi, and the callback function can be cross language.
Also note that the callback function generally (in Delphi component is in the main thread) is the thread created in the callback body, so there is a critical section protection.