写了个程序,如果程序创建了线程,那么想让程序在结束时结束创建的线程~
procedure TForm1.FormDestroy(Sender: TObject);
begin
if brushth<>nil then
begin
brushth.Terminate;
brushth.Destroy;
end;
end;但是好像没用,关掉程序后资源管理器里面程序还在~~~~想不出办法了..
下面是Execute部分,如果登录失败,自行销毁线程,登录成功,循环访问一系列站点,直到程序关闭.procedure ThBrush.Execute;
begin
idhttp:=Tidhttp.Create(nil);
idhttp.ReadTimeout := 20000;
idHTTP.HandleRedirects:=True;
if login(form1.Edit_email.Text,form1.Edit_pass.Text) then
begin
while not Terminated do
begin
visite;
sleep(delaytime);
end;
end else
begin
self.Terminate;
self.Destroy;
end;
idhttp.Free;
end;
各位大侠帮帮小弟...好不容易学了点线程类的皮毛..又卡住了~~
procedure TForm1.FormDestroy(Sender: TObject);
begin
if brushth<>nil then
begin
brushth.Terminate;
brushth.Destroy;
end;
end;但是好像没用,关掉程序后资源管理器里面程序还在~~~~想不出办法了..
下面是Execute部分,如果登录失败,自行销毁线程,登录成功,循环访问一系列站点,直到程序关闭.procedure ThBrush.Execute;
begin
idhttp:=Tidhttp.Create(nil);
idhttp.ReadTimeout := 20000;
idHTTP.HandleRedirects:=True;
if login(form1.Edit_email.Text,form1.Edit_pass.Text) then
begin
while not Terminated do
begin
visite;
sleep(delaytime);
end;
end else
begin
self.Terminate;
self.Destroy;
end;
idhttp.Free;
end;
各位大侠帮帮小弟...好不容易学了点线程类的皮毛..又卡住了~~
self.Terminate;
self.Destroy;那我应该怎么做才能让登录失败的时候(login()返回false)自动销毁创建的线程啊?
self.Terminate;
self.Destroy;
去掉,再把 FormDestroy里的Destroy也去掉就好了~~是不是FreeOnTeminate设置了true,线程会自己destroy啊?因为我不去掉FormDestroy里的Destroy,会出现句柄无效,应该是线程已经destroy了~~回答我这个问题的就送分啊~
线程在结束时就回自己free;
if login(form1.Edit_email.Text,form1.Edit_pass.Text) then千万不要这么做:
1、你在另一个线程访问主线程的数据,要想好同步的问题。
2、在线程类里直接访问form1,从此你就与Form1直接关联起来了。正确做法,向外发布一个异步事件获取想要的数据,Form1接收这个事件,将对这个返回所需要的数据。
如果程序退出可以用api结束线程 TerminateThread
begin
if brushth<>nil then
begin
brushth.Terminate;
brushth.waitfor; //加上这个试试
brushth.free; //我一般都调用free函数的,你也试试吧 brushth.Destroy;
end;
end;