P
PYH
Unregistered / Unconfirmed
GUEST, unregistred user!
TThread类属性FreeOnTerminate,按帮助文件上的说法(很多参考书上也是这样)
,为false时线程对象可以在OnTerminated事件中释放,我的程序代码如下:
主Form很简单,test为自定义的线程类
type
TForm1 = class(TForm)
Button1: TButton;
{创建2个线程}
Button2: TButton;
{释放线程1}
Button3: TButton;
{释放线程2}
procedure Button1Click(Sender: TObject);
procedure tern1(Sender:TObject);
{线程1 Onterminate事件}
procedure tern2(Sender:TObject);
{线程2 Onterminate事件}
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
var
th1:test;{线程对象}
th2:test;
{$R *.DFM}
{Button1创建两个线程,并设置了Onterminate事件}
procedure TForm1.Button1Click(Sender: TObject);
begin
if th1 = nil then
begin
th1:=test.Create(true);
th1.OnTerminate:=tern1;
th1.FreeOnTerminate:=false;
th1.Resume;
end;
if th2 = nil then
begin
th2:=test.Create(true);
th2.OnTerminate:=tern2;
th2.FreeOnTerminate:=false;
th2.Resume;
end;
end;
{线程1 Onterminate事件,显式释放线程}
procedure TForm1.tern1(Sender: TObject);
begin
if th1<>nil then
begin
th1.Free;
th1:=nil;
beep;
end;
end;
{线程2 Ontermiante事件}
procedure TForm1.tern2(Sender: TObject);
begin
if th2<>nil then
begin
th2.Free;
th2:=nil;
beep;
end;
end;
{button2结束线程1}
procedure TForm1.Button2Click(Sender: TObject);
begin
if th1<>nil then
th1.Terminate;
end;
{button3结束线程2}
procedure TForm1.Button3Click(Sender: TObject);
begin
if th2<>nil then
th2.Terminate;
end;
end.
unit2
{线程也很简单,除了一个循环,什么也没做}
type
test = class(TThread)
private
buf1:array[0..8191] of integer;
buf2:array[0..8191] of integer;
{这二个buffer,似乎有魔力}
protected
procedure Execute;
override;
end;
implementation
procedure test.Execute;
begin
while not Terminateddo
begin
end;
end;
end.
运行以上代码,创建线程没问题,结束线程时,第一个正常,结束第二个线程时出
错,是个win32错误,code 为5, 奇怪的是如果将线程中的二个8k大的Buffer去掉,则
一切正常;难道不能在OnTerminate事件中释放线程对象,难道buffer 不能太大,难道
书上说错了!!
使用Freeandnil和先调用对象的free方法,再将其赋成nil,有何不同!!
另:如果先释放第二个线程,再释放第一个线程,也没问题,太怪了!。
困扰多日,请不吝赐教,先谢了!
--------------------------------------------------------------------------------
,为false时线程对象可以在OnTerminated事件中释放,我的程序代码如下:
主Form很简单,test为自定义的线程类
type
TForm1 = class(TForm)
Button1: TButton;
{创建2个线程}
Button2: TButton;
{释放线程1}
Button3: TButton;
{释放线程2}
procedure Button1Click(Sender: TObject);
procedure tern1(Sender:TObject);
{线程1 Onterminate事件}
procedure tern2(Sender:TObject);
{线程2 Onterminate事件}
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
var
th1:test;{线程对象}
th2:test;
{$R *.DFM}
{Button1创建两个线程,并设置了Onterminate事件}
procedure TForm1.Button1Click(Sender: TObject);
begin
if th1 = nil then
begin
th1:=test.Create(true);
th1.OnTerminate:=tern1;
th1.FreeOnTerminate:=false;
th1.Resume;
end;
if th2 = nil then
begin
th2:=test.Create(true);
th2.OnTerminate:=tern2;
th2.FreeOnTerminate:=false;
th2.Resume;
end;
end;
{线程1 Onterminate事件,显式释放线程}
procedure TForm1.tern1(Sender: TObject);
begin
if th1<>nil then
begin
th1.Free;
th1:=nil;
beep;
end;
end;
{线程2 Ontermiante事件}
procedure TForm1.tern2(Sender: TObject);
begin
if th2<>nil then
begin
th2.Free;
th2:=nil;
beep;
end;
end;
{button2结束线程1}
procedure TForm1.Button2Click(Sender: TObject);
begin
if th1<>nil then
th1.Terminate;
end;
{button3结束线程2}
procedure TForm1.Button3Click(Sender: TObject);
begin
if th2<>nil then
th2.Terminate;
end;
end.
unit2
{线程也很简单,除了一个循环,什么也没做}
type
test = class(TThread)
private
buf1:array[0..8191] of integer;
buf2:array[0..8191] of integer;
{这二个buffer,似乎有魔力}
protected
procedure Execute;
override;
end;
implementation
procedure test.Execute;
begin
while not Terminateddo
begin
end;
end;
end.
运行以上代码,创建线程没问题,结束线程时,第一个正常,结束第二个线程时出
错,是个win32错误,code 为5, 奇怪的是如果将线程中的二个8k大的Buffer去掉,则
一切正常;难道不能在OnTerminate事件中释放线程对象,难道buffer 不能太大,难道
书上说错了!!
使用Freeandnil和先调用对象的free方法,再将其赋成nil,有何不同!!
另:如果先释放第二个线程,再释放第一个线程,也没问题,太怪了!。
困扰多日,请不吝赐教,先谢了!
--------------------------------------------------------------------------------