據說 Event(事件對象) 是多線程最原始的同步手段, 我覺得它是最靈活的一個.
Event 對象(的句柄表)中主要有兩個布爾變量, 從它的建立函數中可以看得清楚:
function CreateEvent(
lpEventAttributes: PSecurityAttributes; {安全設置}
bManualReset: BOOL; {第一個布爾}
bInitialState: BOOL; {第二個布爾}
lpName: PWideChar {對象名稱}
): THandle; stdcall; {返回對象句柄}
//第一個布爾為 False 時, 事件對象控制一次後將立即重置(暫停); 為 True 時可手動暫停.
//第二個布爾為 False 時, 對象建立後控制為暫停狀態; True 是可運行狀態.
和其他同類相比, 它的靈活性在於可隨時 "啟動運行"(SetEvent) 和 "暫停運行"(ResetEvent);
甚至還有個 PulseEvent 函數, 能控制執行一次後立即暫停, 很是方便.
本例效果圖:
代碼文件:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
f: Integer; {用這個變量協調一下各線程輸出的位置}
hEvent: THandle; {事件對象的句柄}
function MyThreadFun(p: Pointer): DWord; stdcall;
var
i,y: Integer;
begin
Inc(f);
y := 20 * f;
for i := 0 to 200000 do
begin
if WaitForSingleObject(hEvent, INFINITE) = WAIT_OBJECT_0 then
begin
Form1.Canvas.Lock;
Form1.Canvas.TextOut(20, y, IntToStr(i));
Form1.Canvas.Unlock;
end;
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Repaint; f := 0;
CloseHandle(hEvent); {如果已經創建過}
hEvent := CreateEvent(nil, True, True, nil);
end;
{創建線程}
procedure TForm1.Button2Click(Sender: TObject);
var
ThreadID: DWord;
begin
CreateThread(nil, 0, @MyThreadFun, nil, 0, ThreadID);
end;
{暫停}
procedure TForm1.Button3Click(Sender: TObject);
begin
ResetEvent(hEvent);
end;
{啟動}
procedure TForm1.Button4Click(Sender: TObject);
begin
SetEvent(hEvent);
end;
{啟動後執行一次立即暫停}
procedure TForm1.Button5Click(Sender: TObject);
begin
PulseEvent(hEvent);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '創建 Event 對象';
Button2.Caption := '創建線程';
Button3.Caption := 'ResetEvent';
Button4.Caption := 'SetEvent';
Button5.Caption := 'PulseEvent';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(hEvent);
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 149
ClIEntWidth = 228
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 8
Top = 116
Width = 129
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Button3: TButton
Left = 143
Top = 12
Width = 75
Height = 25
Caption = 'Button3'
TabOrder = 1
OnClick = Button3Click
end
object Button4: TButton
Left = 143
Top = 43
Width = 75
Height = 25
Caption = 'Button4'
TabOrder = 2
OnClick = Button4Click
end
object Button5: TButton
Left = 143
Top = 74
Width = 75
Height = 25
Caption = 'Button5'
TabOrder = 3
OnClick = Button5Click
end
object Button2: TButton
Left = 143
Top = 116
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 4
OnClick = Button2Click
end
end
和前面一樣, 再用 SyncObJS 單元下的 TEvent 類實現一次; 不過它沒有實現類似 PulseEvent 的功能:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses SyncObJS;
var
f: Integer;
MyEvent: TEvent;
function MyThreadFun(p: Pointer): DWord; stdcall;
var
i,y: Integer;
begin
Inc(f);
y := 20 * f;
for i := 0 to 200000 do
begin
if MyEvent.WaitFor(INFINITE) = wrSignaled then
begin
Form1.Canvas.Lock;
Form1.Canvas.TextOut(20, y, IntToStr(i));
Form1.Canvas.Unlock;
end;
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Repaint; f := 0;
if Assigned(MyEvent) then MyEvent.Free;
MyEvent := TEvent.Create(nil, True, True, '');
end;
{創建線程}
procedure TForm1.Button2Click(Sender: TObject);
var
ThreadID: DWord;
begin
CreateThread(nil, 0, @MyThreadFun, nil, 0, ThreadID);
end;
{暫停}
procedure TForm1.Button3Click(Sender: TObject);
begin
MyEvent.ResetEvent;
end;
{啟動}
procedure TForm1.Button4Click(Sender: TObject);
begin
MyEvent.SetEvent;
end;
{啟動後執行一次立即暫停}
procedure TForm1.Button5Click(Sender: TObject);
begin
ShowMessage('TEvent 類沒有提供這個功能'); {我試過用 PulseEvent(MyEvent.Handle) 也不行}
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '創建 Event 對象';
Button2.Caption := '創建線程';
Button3.Caption := 'ResetEvent';
Button4.Caption := 'SetEvent';
Button5.Caption := 'PulseEvent';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyEvent.Free;
end;
end.