现在我在做一个含有线程的程序,其中这个线程是有几个自定义的进程串在一起组成的,进程中包含ADO对象(多线程组件),运行时发现,该线程可以全速运行,挂起,但不能terminate,在试图使用挂起后,再free的方法也不能实现,而且出现程序死掉的情况,线程开始一开始运行就设置freeonterminate:=true,请问高手,这是怎么回事,希望大家尽快帮忙,小弟在这给大家跪下啦~~~~~~
另外本人对文件打印颇有一点心得,谁有兴趣要?

解决方案 »

  1.   

    Delphi线程的Terminate只是设置了Terminated属性而已,需要自己在线程里检测,然后决定是否终止线程。
      

  2.   

    需要在execute里加入判断,如if terminated then exit;调用terminate只是把terminated:=true;并没结束线程
      

  3.   

    多谢各位,我想我明白点了,上面说的那个"进程"是笔误,应该是"过程",不好意思啊,另外整个线程执行周期很长,大概有30多秒,所以循环检查不太合适,还有就是为什么挂起的时候会出现程序无响应的情况呢?大家能不能解释一下?代码如下:
    unit threadunit;interfaceuses
      Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};type
      Tmythread = class(TThread)
      private
        procedure SetName;
      protected
        procedure Execute; override;
      end;implementation
    uses input,publicdeclare,mainform,sysgraphics;
    { Important: Methods and properties of objects in visual components can only be
      used in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure Tmythread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{$IFDEF MSWINDOWS}
    type
      TThreadNameInfo = record
        FType: LongWord;     // must be 0x1000
        FName: PChar;        // pointer to name (in user address space)
        FThreadID: LongWord; // thread ID (-1 indicates caller thread)
        FFlags: LongWord;    // reserved for future use, must be zero
      end;
    {$ENDIF}{ Tmythread }procedure Tmythread.SetName;
    {$IFDEF MSWINDOWS}
    var
      ThreadNameInfo: TThreadNameInfo;
    {$ENDIF}
    begin
    {$IFDEF MSWINDOWS}
      ThreadNameInfo.FType := $1000;
      ThreadNameInfo.FName := 'threadpath';
      ThreadNameInfo.FThreadID := $FFFFFFFF;
      ThreadNameInfo.FFlags := 0;  try
        RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
      except
      end;
    {$ENDIF}
    end;procedure Tmythread.Execute;
    begin
      SetName;
      { Place thread code here }
    try
    begin
    if jsmodel then
      begin
      ybcomp;
      yhbcomp;
      phcomp;
      ltcomp;
      hpcomp;
      dlcomp;
      gzcomp;
      zxcomp;
      dzcomp;
      pscomp;
      zscomp;
      kycomp;
      jsmodel:=false;
    end
    else
      begin
      case procedurebz of
      1:ybcomp;
      2:yhbcomp;
      3:phcomp;
      4:ltcomp;
      5:hpcomp;
      6:dlcomp;
      7:gzcomp;
      8:zxcomp;
      9:dzcomp;
      10:pscomp;
      11:zscomp;
      12:kycomp;
      end;
      end;
    end;
    except
    begin
    mainthread.terminate;
    jsmodel:=false;
    end;
    end;
    form2.button3.Enabled:=true;
    form2.Button2.Enabled:=false;
    form2.Button15.Enabled:=false;
    if form10<>nil then
    begin
    form10.Enabled:=true;
    form10.Button1.Enabled:=true;
    form10.Button2.Enabled:=false;
    form10.Button3.Enabled:=false;
    end;
    jsbz:=0;
    mtime:=0;
    stime:=0;
    end;end.我看delphi帮助里写的好象有问题,terminate根本不是什么boolean型,是个过程啊,真把我给搞糊涂了
      

  4.   

    terminated是bool,terminate是过程设置terminated的属性的
      

  5.   

    你是在DLL中的线程吧,在需要结束的时候加上。Self.OnTerminate(Self);
      

  6.   

    前两天出差了,对不起啊,不是DLL中的线程,就是用的thread对象,楼上说家Self.OnTerminate(Self);是在什么地方加?线程的excute结束的地方?