function CreateThread( lpThreadAttributes: Pointer; dwStackSize: DWORD; {堆栈大小} lpStartAddress: TFNThreadStartRoutine; lpParameter: Pointer; dwCreationFlags: DWORD; var lpThreadId: DWORD ): THandle; stdcall;
代码文件: -------------------------------------------------------------------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //var num: Integer; {全局变量} threadvar num: Integer; {支持多线程的全局变量} function MyThreadFun(p: Pointer): DWORD; stdcall; var py: Integer; begin py := Integer(p); while True do begin Inc(num); with Form1.Canvas do begin Lock; TextOut(20, py, IntToStr(num)); Unlock; end; Sleep(1000); {然线程挂起 1 秒钟再继续} end; end; procedure TForm1.Button1Click(Sender: TObject); var ID: DWORD; begin {借入口函数的参数传递了一个坐标点中的 Y 值, 以让各线程把结果输出在不同位置} CreateThread(nil, 0, @MyThreadFun, Ptr(20), 0, ID); CreateThread(nil, 0, @MyThreadFun, Ptr(40), 0, ID); CreateThread(nil, 0, @MyThreadFun, Ptr(60), 0, ID); end; end. -------------------------------------------------------------------------------- 窗体文件: -------------------------------------------------------------------------------- object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 106 ClientWidth = 180 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 80 Top = 40 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end end