一下子跳到等待函數 WaitForSingleObject, 是因為下面的 Mutex、Semaphore、Event、WaitableTimer 等同步手段都要使用這個函數; 不過等待函數可不止 WaitForSingleObject 它一個, 但它最簡單.
function WaitForSingleObject(
hHandle: THandle; {要等待的對象句柄}
dwMilliseconds: DWord {等待的時間, 單位是毫秒}
): DWord; stdcall; {返回值如下:}
WAIT_OBJECT_0 {等著了, 本例中是: 等的那個進程終於結束了}
WAIT_TIMEOUT {等過了點(你指定的時間), 也沒等著}
WAIT_ABANDONED {好不容易等著了, 但人家還是不讓咱執行; 這一般是互斥對象}
//WaitForSingleObject 的第二個參數一般給常數值 INFINITE, 表示一直等下去, 死等.
WaitForSingleObject 等待什麼? 在多線程裡就是等待另一個線程的結束, 快來執行自己的代碼; 不過它可以等待的對象可不止線程; 這裡先來一個等待另一個進程結束的例子, 運行效果圖:
代碼文件: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
hProcess: THandle; {進程句柄}
{等待一個指定句柄的進程什麼時候結束}
function MyThreadFun(p: Pointer): DWord; stdcall;
begin
if WaitForSingleObject(hProcess, INFINITE) = WAIT_OBJECT_0 then
Form1.Text := Format('進程 %d 已關閉', [hProcess]);
Result := 0;
end;
{啟動一個進程, 並建立新線程等待它的結束}
procedure TForm1.Button1Click(Sender: TObject);
var
pInfo: TProcessInformation;
sInfo: TStartupInfo;
Path: array[0..MAX_PATH-1] of Char;
ThreadID: DWord;
begin
{先獲取記事本的路徑}
GetSystemDirectory(Path, MAX_PATH);
StrCat(Path, 'notepad.exe');
{用 CreateProcess 打開記事本並獲取其進程句柄, 然後建立線程監視}
FillChar(sInfo, SizeOf(sInfo), 0);
if CreateProcess(Path, nil, nil, nil, False, 0, nil, nil, sInfo, pInfo) then
begin
hProcess := pInfo.hProcess; {獲取進程句柄}
Text := Format('進程 %d 已啟動', [hProcess]);
CreateThread(nil, 0, @MyThreadFun, nil, 0, ThreadID); {建立線程監視}
end;
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 124
ClIEntWidth = 241
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 = 88
Top = 56
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end