FreeOnTerminate不能释放线程
delphi7 多线程学习--子线程释放
子线程一定要手工释放?不能自动释放?
下面是主 子线程代码
unit UThreadTest;
interface
uses
Classes,SysUtils,ADODB,ActiveX;
type
TMainthr = class(TThread) //主线程
private
{ Private declarations }
adocon : TADOConnection;
protected
procedure Execute; override;
public
name : String;
ss : TStrings;
isStop : Boolean;
Constructor Create;
destructor Destroy; override;
end;
TSubthr = class(TThread) //子线程
private
{ Private declarations }
adocon : TADOConnection;
protected
procedure Execute; override;
public
name : String;
ss : TStrings;
isStop : Boolean;
Constructor Create;
destructor Destroy; override;
end;
implementation
{ TMainthr }
Constructor TMainthr.Create;
begin
inherited create(true); //生成是阻赛的的
adocon := TADOConnection.Create(nil);
ss := TStringList.Create;
end;
destructor TMainthr.Destroy;
begin
inherited;
FreeAndNil(adocon) ;
FreeAndNil(ss);
CoUninitialize;
end;
procedure TMainthr.Execute;
var
aSubthr : TSubthr;
i : Integer;
begin
i:= 0;
while not Terminated do
begin
aSubthr := TSubthr.Create;
aSubthr.FreeOnTerminate := true;
aSubthr.name := 'Subthr';
aSubthr.Resume;
if Assigned(aSubthr) then
begin
aSubthr.name :='bbb';
ss.Add('Subthr还未释放');
end;
ss.Add(name + IntToStr(i));
if i>50000 then
begin
i:=0;
ss.Clear;
end;
i := i+ 1;
Sleep(500);
end;
end;
{ TSubthr }
Constructor TSubthr.Create;
begin
// inherited create(true); //生成是阻赛的的
inherited Create(true);
FreeOnTerminate := true;
CoInitialize(nil);
adocon := TADOConnection.Create(nil);
ss := TStringList.Create;
end;
destructor TSubthr.Destroy;
begin
inherited;
FreeAndNil(adocon) ;
FreeAndNil(ss);
end;
procedure TSubthr.Execute;
var
i : Integer;
begin
for i := 0 to 20 do
begin
Sleep(10);
end;
i :=0;
end;
end.
var
aMainThr : TMainthr;
procedure TForm1.btn16Click(Sender: TObject);
begin
if not Assigned(aMainthr) then
begin
aMainthr := TMainthr.Create;
// ffa.FreeOnTerminate:=True;
// mmo1.Text := ffa.ss.Text;
// ffa.ss:=mmo1.Lines;
// ffb.aWriteLog := wrtieLog;
aMainthr.name := 'MainThr';
aMainthr.Resume;
end;
end;
