CnPack Forum » 技术板块灌水区 » bpl内线程怎么退不出来


2007-8-25 18:21 niaoge
bpl内线程怎么退不出来

在运行时 bpl内
destructor TxxThread.Destroy;
begin
  //下面这段话...到结束 如果不加的话,exe关闭时就出错,加的话,又退不出来 ,请版主看看
  if ModuleIsLib then
  begin
    if not FreeOnTerminate then
    begin
      FreeOnTerminate := true;
      WaitFor;
      Exit;
    end;
  end;
  //结束
  Terminate;
  PMessages;
  WaitFor;
  inherited;
end;

2007-8-27 22:19 skyjacker
我不知道你说的 bpl内线程 与普通线程有什么区别。

我想知道你想在 TxxThread.Destroy 里干什么。

2007-8-29 01:29 niaoge
destructor TxxThread.Destroy;
begin
  //下面这段话...到结束 如果不加的话,exe关闭时就出错,加的话,又退不出来 ,请版主看看
  if ModuleIsLib then
  begin
    if not FreeOnTerminate then //判断是不是结束时退出
    begin
      FreeOnTerminate := true;   //让结束时退出
      WaitFor;                            //暂先不退出,等待这个线程的事情结束后由于FreeOnTerminate := true会再次触发 Destroy 以便执行下面代码
      Exit;
    end;
  end;
  //结束
  Terminate;
  PMessages;
  WaitFor;
  inherited;
end;

这是rem内的,怎么改都退不出来,我只好把多线程的这块分出来,不放在bpl内,其它的合做成一个静态包bpl
以前不相信多线程不能打包到bpl内,通过自己做才相信了这点,不是退不出来,就是内存报错,
注:这段代码放在exe内,不会报错

[[i] 本帖最后由 niaoge 于 2007-8-29 01:32 编辑 [/i]]

2007-8-29 14:27 Passion
WaitFor的时候会再次触发Destroy?

2007-10-10 05:05 AdamWu
Because wait for will not set Terminated flag before calling WaitForSingleObject API,  so your program will be stuck just before Exit.
Instead, I think you should call Terminate and then call Exit.

页: [1]
查看完整版本: bpl内线程怎么退不出来


Powered by Discuz! Archiver 5.0.0  © 2001-2006 Comsenz Inc.