CreateThread 是 Win32 API 函數.
其定義如下:
function CreateThread(
lpThreadAttributes: Pointer; {安全设置}
dwStackSize: DWORD; {堆栈大小}
lpStartAddress: TFNThreadStartRoutine; {入口函数}
lpParameter: Pointer; {函数参数}
dwCreationFlags: DWORD; {启动选项}
var lpThreadId: DWORD {输出线程 ID }
): THandle; stdcall; {返回线程句柄}
簡化一下, 只關注其中幾個參數和返回值:
1. 返回值為新線程的 Handle;
2. lpStartAddress ; 入口函数(即在線程中要進行處理的內容)地址 ; 標準定義為: function MyThreadFun(p: Pointer): Integer; stdcall;
3. dwCreationFlags : 是否設置線程創建後馬上suspend 還是立即執行; true = 掛起. 使用ResumeThread方法恢復執行線程;
4. lpThreadId : 線程ID.(唯一的) .與句柄不同. 可能有多個句柄指同一個線程.
簡單的應用:
var
hThread : THandle;
hThread := CreateThread(nil, 0, @MyThreadFun, nil, CREATE_SUSPENDED, ID);
CreateThread 對應的函數為:
function BeginThread(SecurityAttributes: Pointer; StackSize: LongWord; ThreadFunc: TThreadFunc; Parameter: Pointer;
CreationFlags: LongWord; var ThreadId: LongWord): Integer;