function CreateThread(
lpThreadAttributes: Pointer;
dwStackSize: DWORD;
lpStartAddress: TFNThreadStartRoutine;
lpParameter: Pointer; {入口函数的参数}
dwCreationFlags: DWORD;
var lpThreadId: DWORD
): THandle; stdcall;
The parameter of the thread entry function is an untyped pointer (pointer) that can be used to specify any data; This example passes the coordinates of the mouse-click form to the entry function of the thread, creating a thread each time the form is clicked.
Run Effect chart:
Code files: Unit Unit1
Interface
uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs
Type
TForm1 = Class (Tform)
Procedure Formmouseup (sender:tobject; Button:tmousebutton;
Shift:tshiftstate; X, Y:integer);
End;
Var
Form1:tform1
Implementation
{$R *.DFM}
var
pt:tpoint; {This coordinate point will be passed to the thread as a pointer, it should be global}
Function Mythreadfun (p:pointer): Integer; stdcall;
var
I:integer;
Pt2:tpoint; {Because the points given by the pointer parameters are changing at any time, you need to save the local variables of the thread}
Begin
Pt2: = PPoint (p) ^; Convert
for I: = 0 to 1000000 does
begin
with Form1.canvas do begin
Lock;
TextOut (pt2. X, Pt2. Y, IntToStr (i));
Unlock;
End;
End;
Result: = 0;
End;
Procedure Tform1.formmouseup (sender:tobject; Button:tmousebutton;
Shift:tshiftstate; X, Y:integer);
var
id:dword
Begin
PT: = Point (X, Y);
CreateThread (nil, 0, @MyThreadFun, @pt, 0,ID);
{This notation is better understood in fact, because PPoint is automatically converted to pointer}
//createthread (nil, 0, @MyThreadFun, pointer (@pt), 0, ID);
E nd
End.