在线程代码里面FreeOnTerminate:= True 只是设置个标志,
begin
{ Place thread code here }
FreeOnTerminate:=false;//不释放
while truedo
begin
Synchronize(ShowInMemo);
Suspend;
//在这里就停住等下一次Click了
end ;
end;
//执行本语句后才会Terminate,也才会Free,但本句永远也到不了
type
TRepeatThread = class(TThread)
private
FButton: TButton;
public
constructor Create(AButton: TButton);
procedure UpdataCaption;
procedure Execute;
override;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
ButtonTread: TRepeatThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TRepeatThread.Create(AButton: TButton);
begin
FButton := AButton;
inherited Create(False);
end;
procedure TRepeatThread.Execute;
begin
FreeOnTerminate := True;
repeat
Synchronize(UpdataCaption);
Suspend;
if Terminated then
Break;
until false;
end;
procedure TRepeatThread.UpdataCaption;
begin
FButton.Caption := IntToStr(random(MaxInt));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if ButtonTread = nil then
ButtonTread := TRepeatThread.Create(TButton(Sender))
else
// ButtonTread.Resume;
ButtonTread.Suspended := False;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if ButtonTread <> nil then
begin
ButtonTread.Terminate;
ButtonTread.Resume;
ButtonTread := nil;
end;
end;
// 简单就不多说了。