標籤:
轉載自:萬一的部落格
一下子跳到等待函數 WaitForSingleObject,是因為下面的Mutex、Semaphore、Event、WaitableTimer等同步手段都要使用這個函數。不過等待函數可不止WaitForSingleObject這一個,但是它是最簡單的
function WaitForSingleObject( hHandle: THandle; //要等待的物件控點 dwMillseconds: DWORD; //等待的時間,單位是毫秒): DWORD; srdcall;
傳回值說明如下
WAIT_OBJECT_0 {等著了,本例中是:等的那個進程終於結束了}WAIT_TIMEOUT {等過了點(你指定的時間),也沒等著}WAIT_ABANDONED {好不容易等著了,但是人家還是不讓咱執行;這一般是互斥對象}
//WaitForSingleObject 的第二個參數一般給常數值 INFINITE, 表示一直等下去, 死等.
WaitForSingleObject等待什嗎?在多線程就是等待另一個線程的結束,快來執行自己的代碼;不過他可以等待的對象可不止線程;這裡先來一個等待另一個進程結束的例子,運行
代碼如下
unit Unit1;interfaceuses 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 endend
Delphi多線程編程(9)--認識等待函數WaitForSingleObject