Delphi foundation: callback function and its use

xiaoxiao2021-03-06  68

Delphi Basics: An overview of the callback function and its use 1 callback function is such a mechanism: the caller is initializing an object (here the object is generally referred to, including objects in OOP, global functions, etc.) Pass to the object while transmitting a caller can access the function address to the object. This function is a notification agreement between the caller and the caller. When the agreed event occurs, the called is called by the callback function address when the agreed event occurs.

This way, the caller is in a thread, the caller being in another thread.

news:

The message can also be seen as some form of callback because the message is also initialized by the caller to pass a handle and a message number to the caller, and send a message to the caller when the agreed event occurs.

In this way, the caller is in the main thread, and the caller is in the main thread or the working thread.

Delphi event model:

There are many visual components in the VCL of Delphi, all of which use event models, such as TFORM's oncreate events, which specifies event functions when designing, when runtime events are triggered, the event function specified in design is called.

In mechanisms, the Delphi event model is the same as the callback. However, some differences in the specific form, the pure callback function is in the form of a global function, and the Delphi event is in the form of an object method, that is, the following adjustment function type can be defined.

Type

Tcallbackfunc = procedure (PDATA: POINTER) OF Object;

2 instructions for callback function

The callback function is mainly used in two occasions. The first is that some Windows API requirements use the callback function as its parameter address, and the other is a function of a user to use a callback function as its parameters. Address, for functions of the user's definition, usually used when the function in the dynamic connection library is called.

There are several steps to use a callback function:

1. Define a callback function type, and there is no difference in the definition of the general function process, but its definition must meet the function requirements of the callback function as needed, the only difference is that it must be declared behind the Windows standard after the function or process definition. transfer;

example:

Type

THDFUNCTION = Function (I: Integer; s: string): integer; stdcall;

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

New Post(0)