我创建了一个线程。但是发现线程执行完后,系统没有释放指定的线程及资源,即没有执行到指定的destory析构函数,请问大侠们是怎么回事呀?下面是线程创建的源代码,简化了
threadvar
  ii:integer;
  type
    TVfgProcess = class(TThread)
    private
      TcpConn: TIdTcpServerConnection;
      AThread: TIdPeerThread;
      CommPkg:String;
      VarList, SimulaDataList:TStringList;        //存放在各个方法间传递的变量和值,模拟值,
      TermFmtList, TermDataList:TStringList;      //存放终端请求数据和数据格式
      DbList:TStringList;
      BankFmtList,BankDataList:TStringList;       //存放后台的请求包格式和模拟数据
      tcpClient: TIdTCPClient;
    protected
      procedure Execute; override;
      procedure FreeSrc;
      procedure SB_Process();
      procedure RB_Process();
    public
      over_flag:boolean;
      constructor Create(pAThread: TIdPeerThread;paCommPkg:string);
      destructor Destory();
    end;
    
implementationuses
  untVFMonitor;constructor TVfgProcess.Create(pAThread: TIdPeerThread;paCommPkg:string);
begin
  inherited create(true);
  inc(gThreadCount);        //线程数加1  over_flag := false;
end;procedure TVfgProcess.FreeSrc;
begin
   //释放相关资源
end;destructor TVfgProcess.Destory();
begin
  gThreadCount := gThreadCount-1;
  FreeSrc;
end;
//下面是调用线程的部分
th := TVfgProcess.Create(AThread, TermRequestData);
    th.Resume;
    lCount := 0;
    while not th.over_flag do
    begin
      inc(lCount);
      if lCount>1000 then break;
      sleep(100);
    end;
    th.Free;
本来是没有th.Resume后面的部分的,但是加了之后,还是不会执行到线程的destory
请问是怎么回事,应该如何设计或调用,才能使destory被执行?
就算在th.Free之前执行 th.Destory,结果也是一样,destory的代码并不会执行

解决方案 »

  1.   

    前提是要 Execute 是执行结束的
      

  2.   

    excute中所有代码用 
    try 
      try
        .......
      except 
        .....
      end;
    finally
      over_flag := true;
    end
    括起来了的
      

  3.   

    th.over_flag 因为作为 while 条件, 可能会被优化到寄存器的
      

  4.   

    FreeOnTerminate:= TRUE;
    这一句应该写在什么地方?什么条件下该句会起作用?
      

  5.   

    在线程结束的时候把它释放掉 看帮助Set FreeOnTerminate to True if you don抰 want to explicitly destroy threads after they finish executing. When FreeOnTerminate is False, the thread object must be explicitly destroyed in application code.