一个线程如果退出时,我们习惯写下如下代码:

AThread.Terminate;
...
AThread.WaitFor;
AThread.Free;

但在DLL中调用会在WaitFor的地方出现死等的现象,线程无法退出。

改为下面的代码可以解决:

AThread.Suspended:=False;//线程如果没运行强制运行
while not AThread.Started do//线程如果没有处于已启动状态,等待它启动完成
    Sleep(10);
AThread.Terminate;//通知线程结束
//等待线程退出
while not AThread.Finished do
    Sleep(10);
//释放线程对象
AThread.Free;

对于Delphi早期版本,TThread的Finished属性并没有公开,此时,我们需要一些非常手段来处理下。

如Delphi 2007,我们通过跟踪,可知道其FFinished的原始地址为Integer(Self)+16,即可以如此判断:

PBoolean(Integer(AThread)+16)^

 

原文地址:http://hi.baidu.com/chineseswish/item/c002afdd81a3b3b232db9046

 

相关文章:

  • 2021-12-06
  • 2019-11-25
  • 2021-07-28
  • 2021-09-03
  • 2021-09-14
  • 2022-12-23
  • 2021-12-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
相关资源
相似解决方案