窗體文件: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.