一个简单的例程:
unit Unit1;
interface
uses
Windows, Classes;
TThreadA = class(TThread)
protected
procedure Execute;override;
public
constructor Create;
end;
implementation
procedure TThreadA.Execute;
begin
while not self.Terminateddo
begin
...//线程中的计算,中间也可以穿插对Terminated的判断
end;
end;
constructor TThreadA.Create;
begin
inherited Create(False);
FreeOnTerminate := True;
end;
end.
在主窗体关闭时
//TThreadA的实例化
a := TThreadA.Create;
procedure TFrmMain.FormClose(Sender: TObject;
var Action: TCloseAction) ;
begin
a.Terminate;//这样就可以了
end;