给你个DEMO;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
CS:TRTLCriticalSection;
implementation
{$R *.DFM}
uses unit2,unit3;
var thread1:mymath1;
thread2:mymath2;
procedure TForm1.Button1Click(Sender: TObject);
begin
thread1.resume;
thread2.resume;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
initializeCriticalSection(cs);
thread1:=mymath1.create;
thread2:=mymath2.create;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
thread1.suspend;
thread2.suspend;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
thread1.destroy;
thread2.destroy;
thread1:=mymath1.create;
thread2:=mymath2.create;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
thread1.destroy;
thread2.destroy;
Application.Terminate;
end;
end.
//////////////////////////////////////////
unit Unit2;
interface
uses
Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,unit1;
type
mymath1 = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor create;
end;
implementation
constructor mymath1.create;
begin
inherited create(true);
end;
procedure mymath1.Execute;
var i:integer;
begin
EnterCriticalSection(cs);
for i:=1 to 10000do
form1.edit1.text:=inttostr(i);
LeaveCriticalSection(CS);
end;
end.
/////////////////////////////////////////
unit Unit3;
interface
uses
Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,unit1;
type
mymath2 = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor create;
end;
implementation
constructor mymath2.create;
begin
inherited create(true);
end;
procedure mymath2.Execute;
var i:integer;
begin
EnterCriticalSection(cs);
for i:=1 to 20000do
form1.edit2.text:=inttostr(i);
LeaveCriticalSection(CS);
end;
end.