//////////////////////
/////TestEvent.dpr
program TestEvent;
uses
Forms,
MainFrm in 'MainFrm.pas' {frmMain},
Thrd in 'Thrd.pas';
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end.
/////////////////////////////////////
///Thrd.pas
unit Thrd;
interface
uses
Classes, Windows, syncobjs;
type
TTestThrdA = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
TTestThrdB = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
const
FULL = 100;
var
aryInt: array[1..FULL] of integer;
EA, EB: TEvent;
ThrdA: TTestThrdA;
ThrdB: TTestThrdB;
implementation
uses
MainFrm;
procedure TTestThrdA.Execute;
var
i: Integer;
begin
while not Terminateddo
begin
if EA.Waitfor(INFINITE) = wrAbandoned then
Exit;
for i := 1 to FULLdo
aryInt := Random(100);
EB.SetEvent;
end;
end;
procedure TTestThrdB.Execute;
var
i: Integer;
begin
while not Terminateddo
begin
if EB.Waitfor(INFINITE) = wrAbandoned then
Exit;
frmMain.Canvas.MoveTo(0, 0);
for i := 1 to FULLdo
begin
frmMain.Canvas.LineTo(i * 2, aryInt);
Sleep(10);
end;
EA.SetEvent;
end;
end;
end.
////////////
////MainFrm.pas
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, syncobjs;
type
TfrmMain = class(TForm)
btn: TButton;
procedure btnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
b: BOOL;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses
THrd;
{$R *.DFM}
procedure TfrmMain.btnClick(Sender: TObject);
begin
if not b then
begin
Self.Repaint;
EA := TEvent.Create(nil,false ,false, '');
EB := TEvent.Create(nil,false ,false, '');
ThrdA := TTestThrdA.Create(True);
ThrdB := TTestThrdB.Create(True);
ThrdA.FreeOnTerminate := True;
ThrdB.FreeOnTerminate := True;
ThrdA.Resume;
ThrdB.Resume;
EA.SetEvent;
btn.Caption := 'END';
end
else
begin
EA.Free;
EB.Free;
ThrdA.Terminate;
ThrdB.Terminate;
btn.Caption := 'begin
';
end;
b := not b;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
b := False;
btn.Caption := 'begin
';
end;
end.
////////////////////////////
/////MainFrm.dfm
object frmMain: TfrmMain
Left = 160
Top = 113
Width = 498
Height = 351
Caption = 'frmMain'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object btn: TButton
Left = 64
Top = 240
Width = 75
Height = 25
Caption = 'begin
'
TabOrder = 0
OnClick = btnClick
end
end
//没仔细调试,但可运行。