主程序会关闭所有子线程,如一定要自己关,正常情况下应该用TThread的Terminate来停止线程,在线程的Execute中的循环里每次判断Terminated是否为true是则退出即可。强行终止线程可以用TerminateThread这个API,但它不会FREE线程类,如果手工FREE则会因为线程已终止而出错,建议不要这样用。

解决方案 »

  1.   

    谢谢raptor,你的:在线程里判断terminated为true,是不是在主程里调用threadid.terminate
    在线程里又怎么调用判断terminated呢?我不知道在本线程里怎么读出自己的属性,在java里
    使用this.xxxx可以查看修改本身属性,在delphi里是什么?线程里退出的指令又是什么?exit?
    return?break?
      

  2.   

    如:
    Procedure TMyThread.Execute;
    Begin
      While ( Not Terminated ) AND ( your condition ) Do
      Begin
        your code...
      End;
    End;Execute 执行完后线程就终止了,如果FreeOnTerminate 属性为 true 则线程终止时,同时自动释放线程对象。
    Delphi里与JAVA的this对应的是Self