callback function and Delphi event model

Source: Internet
Author: User
Tags thread

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.