我线程是这么设置的
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一直不能退出?难道是死锁发发生?怎么办?

解决方案 »

  1.   

    while内部有无法退出的代码,死循环
      

  2.   

    你不能在线程本身检测它自己到底退没退出,因边即使Terminated=True,其excute可能还在执行.参考置顶贴,退出方式比较好
      

  3.   

    你不能在线程本身检测它自己到底退没退出,因边即使Terminated=True,其excute可能还在执行.现在就是这个情况:
    a.Terminate ;后线程一直没有正常结束
    所以:
    while not Terminated do
     begin end;
    就一直无法结束。说明:while not Terminated do
     begin end;
    里面没任何代码了。
      

  4.   

    FreeOnTerminate := true;是线程执行完了就释放。
    我觉得应该用个全局变量来替换
    while not Terminated do
      

  5.   

    一般使用格式是这样的:
    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;
      

  6.   

    //主线程退出等待
      if FCollectThread <> nil then
      begin
        if not FCollectThread.ExitThread(6000) then
          Application.MessageBox('线程退出超时', 
            MB_OK + MB_ICONSTOP + MB_TOPMOST);
      end;
      

  7.   

    LZ的意思是自己等自己?这个就不知道了
    线程中只有Excute()的部分代码是在“线程”中运行的,其它代码都是在主线程中运行的
      

  8.   

    线程是一个单独的执行体,他要么自己执行完了退出,当然也可以通过其他线程通知他退出,比如其他线程将delphi thread.terminated = true,只要在execute判断就可以了,异常肯定是资源没处理好。
      

  9.   

    楼主的代码
    ......
    Terminate ;
    while not Terminated do
     begin end;VCL的代码
    procedure TThread.Terminate;
    begin
      FTerminated := True;
    end;把这些代码放在一起,能看出问题所在吗
      

  10.   


    看看这个属性符合你的要求吗OnTerminate
      

  11.   

    Write an OnTerminate event handler to execute code after the thread finishes executing. The OnTerminate event handler is called in the context of the main thread, which means methods and properties can be called freely.