Tonance function in Delphi

zhaozj2021-02-17  52

Tonance function in Delphi

The callback function callback function is a notification method between two objects. The notified person is in advance that the processing function is at a certain situation, and the notification is initiated at this time to call the processing function to complete the callback. For example, A is a function, b is a timer object, A wants to receive a notification of B at the specified time to receive B, the execution steps are as follows: // The following is Delphi pseudo code TPROC = procedure (); // callback function Type definition, global function pointer procedure a (); // actual callback function TTIMER = Class // Timer class Private FPA: TProc; public procedure TTITCALLBACK (PA: TPROC); procedure proc (); begin if assigned FPA) The FPA (); // The agreed time is here, the callback A end; end; // Execute the process var B: TTIMER; Begin ... B.SetCallback (a); // Prior Connection processing function a ... b.Proc ); // A call in this method occurs, callback A ... END; the TPROC in the above is a definition of global functions, and the global function can only use global variables and local variables when the global function is called, and cannot be used as an object. To call the method such as Self.Call, it is really inconvenient to use; and the global function will also pollute the namespace, it is not a good habit. What if we want to pass an object in B.SETCALLBACK ()? It is not difficult to achieve him, mainly to use TOBJECT.METHODDRESS. Remove the definition of the function A, change to class Ta: ta = classpublish // Note: Must be public procedure callback (); end; // execution process var B: TTIMER; A: Ta; recomgin ... B.SetCallback (a. MethodAddress ('Callback')); // Prior Constitution Processing Function A.Callback ... B.Proc (); // The call will occur at a time, then callback A ... END; this is ok. One thing note, when the method of declaring TA Callback, must be declared as publicished, otherwise an error will occur, why? The original TOBJECT.METHODDRESS method can only take out the Published type method. If there is no declaration as a published, A.MethodDress ('Callback') will return empty, resulting in an error. Also note that using this method is to declare the callback function as a parameter because this method parameters cannot be passed to the callback function. With the two shortcomings, it must be defined as a method of published types, and parameters cannot be passed, such a limit is also very large. There is another way to define the callback function, he can solve this problem. That is to change the definition of TPROC to: tproc = procedure () of object; then, the backup function can support any object method, do not call a.MethodDress ('Callback') when calling B.SetCallback. The function address is changed to B.SetCallback (a.callback). If the above three callback modes, each of them is advisable, which is needed to decide which one to use according to actual needs.

The callback function in the Windows API has some functions in the Windows API using a callback function, and his running mechanism is similar to the callback mechanism in the Delphi mentioned above. For example, CreateThread, Setwindowlong, SetWindowsHook and other functions. The corresponding callback function can be defined as the following form: Procedure ThreadFunction (PTR: POINTER); stdcall; // thread function function messageCallbackfunc (WND: hwnd; msg, wparam, lparam: longint): longint; stdcall; // Window message processing callback The callback function mentioned in the function callback function is in addition to the thread function, in addition to the thread function, the callback man and the callback are in the same thread, which is like the manager tells many workers, managers each time I have told all workers what (loop processing), and I have been inquiry if I have finished something (agreed incident), until the worker answers is that workers call (initiated callback) to managers, manage This work records (callback processing) during the process of non-hanging calls. This step is really ridiculous. The manager will be tired, and there is no efficiency, the process can be changed to this, the manager tells all the workers at the beginning, then he can go to meet customers or have a cup of tea, when a worker After completing the job, call the manager, and then record the work recorded the case. Suppose the manager has no electricity to the worker or forgets that it is troublesome, so the worker is waiting. The situation can also be changed, that is, the manager tells the worker email address, the manager handles the email when it is idle, and found that some workers have completed his work, he will record the work record. As above is three thread modes of the callback function: 1, initiate a callback member (subject to worker) and the callbacker (class rather) in the same thread. 2, launch the callbacker and the pseudor belong to different threads, initiating the turning tuner must wait for the callback to handle completion to return. 3, launch the callback tester and the pseudor belong to different threads, and the initiator is not needed to wait for the callback to complete the completion of the callback. March 12, 2005

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

New Post(0)