我线程是这么设置的
procedure a.Execute ;
begin
try
FreeOnTerminate := true;
......finally
a.Terminate ;
while not Terminated do
begin end;end;
end;有的时候会在while not Terminated do 这个循环里不退出了。导致线程就无法结束。我的本意使得线程确实结束后才退出a.Execute 。什么情况下会导致这个while not Terminated do一直不能退出?难道是死锁发发生?怎么办?
procedure a.Execute ;
begin
try
FreeOnTerminate := true;
......finally
a.Terminate ;
while not Terminated do
begin end;end;
end;有的时候会在while not Terminated do 这个循环里不退出了。导致线程就无法结束。我的本意使得线程确实结束后才退出a.Execute 。什么情况下会导致这个while not Terminated do一直不能退出?难道是死锁发发生?怎么办?
解决方案 »
- 求 datasnap三层 数据库连接池
- 为何生成的程序名改成中文后,最小化时,地址栏的文件名有问号?
- 请教:想用D7做个电子书,各位高手帮助一下,百分送上
- 请教各位一个编程方面的错误提示问题
- delphi 7 中sql的一些问题...
- 操作http://..../123.mdb 这样的数据库可以吗?
- delphi开发WebService问题“如何从客户端更改服务器端SQLConnection属性”(内详)
- 如何取得WAVE文件的长度?
- 长沙的朋友请过来看看~!
- ????"帮助"文件怎么做??
- PDF SDK API(Delhi版本)--hot
- C#使用WeifenLuo.WinFormsUI.Docking.dll 实现屏蔽拖拽功能!
a.Terminate ;后线程一直没有正常结束
所以:
while not Terminated do
begin end;
就一直无法结束。说明:while not Terminated do
begin end;
里面没任何代码了。
我觉得应该用个全局变量来替换
while not Terminated do
procedure a.Execute ;
beginFreeOnTerminate := true;while not Terminated do
begin
.....
//任务代码在这里
end;end;简单的退出方法:
主线程结束命令:
theThread.Terminate;
Sleep(XXXX);//延时傻等结束(对于比较简单的线程这样也够了)
.......//然后释放线程用到的资源我的一个示例(参考置顶贴写的):
procedure TCollectThread.Execute;
var
i,n:Integer;
begin
FreeOnTerminate:=True; while True do
begin
if PeekMessage(Msg,0,0,0,PM_REMOVE) then
begin
if Msg.message = WM_COLLECT_STOP then
begin
FQuitEvent.SetEvent;
Break;
end;
end; //........... if IsStop then
begin
Continue;
end; for i:=0 to MaxCount-1 do
begin
if IsStop then
begin
Break;
end; // .............. for n:= 0 to COMM_REPEAT_NUM-1 do
begin if IsStop then
begin
Break;
end; try
//.............
except
//.............
end;
end;
//........... end; if IsStop then
begin
Continue;
end;
//...............
end;
end;function TCollectThread.ExitThread(AWaitTime: Integer): Boolean;
begin
Result:=True;
FStopEvent.SetEvent;
FQuitEvent:=TEvent.Create(nil,True,False,'');
PostThreadMessage(ThreadID,WM_COLLECT_STOP,0,0);
if FQuitEvent.WaitFor(AWaitTime) = wrTimeOut then
Result:=False;
FQuitEvent.Free;
end;function TCollectThread.IsStop: Boolean;
begin
Result:=False;
if FStopEvent.WaitFor(5) = wrSignaled then
Result:=True;
end;
if FCollectThread <> nil then
begin
if not FCollectThread.ExitThread(6000) then
Application.MessageBox('线程退出超时',
MB_OK + MB_ICONSTOP + MB_TOPMOST);
end;
线程中只有Excute()的部分代码是在“线程”中运行的,其它代码都是在主线程中运行的
......
Terminate ;
while not Terminated do
begin end;VCL的代码
procedure TThread.Terminate;
begin
FTerminated := True;
end;把这些代码放在一起,能看出问题所在吗
看看这个属性符合你的要求吗OnTerminate