我在程序中创建了好几个线程中,线程的执行函数(EXECUTE)里是定时执行某个操作,在这个定时过程中,我程序关闭了,这时候怎么不会执行线程的Destroy事件啊?高手帮忙啊!

解决方案 »

  1.   

    那你在程序结束前先设置线程teminated;
    然后调用WaitForSingleObject等待这个线程结束,我就这么干的.
      

  2.   

    var
      A: TMyThread;procedure Form1.Button1Click(Sender: TObject);
    begin
       A := TMyThread.Create(False);
    end;以上名字为A的线程被我反复创建了5次,这时在程序结束前我怎么来区分这5个线程啊,通过名字A不行啊望指教!
      

  3.   

    楼上的写法,是会造成内存泄漏的.第一次点击Button1,没有问题;但第二次,第三次...以后每点一次,就泄漏一次内存,因为上一次Create的TMyThread没有释放;创建对象,最好不要写在button的Click事件中吧.可以用数组来处理var
      aa:array [1..5] of TMyThread;//定义一个数组
    在Form的OnCreate事件中var
      i:integer;
    begin
      for i:=1 to 5 do
         aa[i]:= TMyThread.Create(False); //创建5个线程
    在Form的OnClose事件中
      for i:=1 to 5 do
      begin
         aa[i].Terminate; //结束线程
         aa[i].Waitfor;
      end;
      

  4.   

    楼上的,我用你这个方法试过,但是执行到waitfor这里的时候程序就会定在那里,
    注:
      1).Execute 里我有写  while not Terminated do.....
      2).我的Execute里写有定时操作的,是通过GetTickCount来的,
    望指教
      

  5.   

    to do2008(事情做好了,才是事情)你的方法中,在最后FORMCLOSE事件里
    for i := 1 to 5 do
    begin
      aa[i].Terminate;
      aa[i].WaitFor //这里会出错,错误 是:Thread Error 句柄无效
    end;
      

  6.   

    for i := 1 to 5 do
    begin
      aa[i].Terminate;
      //aa[i].WaitFor //注释掉这一句,不要了  //
    end;
      

  7.   

    我用
    for i := 1 to 5 do
    begin
      aa[i].Terminate;
    end;
    结束线程时,线程里的Destroy事件不会执行。我设了FreeOnTerminate := True;请问哪个高手啊,设了FreeOnTerminate := True的情况下,怎么等待一个线程结束,并让他执行线程的Destroy事件????
      

  8.   

    你的Destroy中的代码是怎么写?
    线程类TThread是没有Destroy事件,只有OnTerminate事件,
    Destroy是类TThread的析构函数,LZ可能搞混了.当FreeOnTerminate := True的情况,调用Terminate方法这后,就不要再调用线程的其它任何时候方法
    ...
      aa[i].Terminate;
      //aa[i].WaitFor //这一句不要,不必再调用了
    ...参考自在线帮助:When FreeOnTerminate is true, the Execute method may run and then free the thread before your application can execute the next line of code. Thus, you should not call any methods of the thread object when FreeOnTerminate is true unless you create the thread in a suspended state.当FreeOnTerminate := false的情况,用free这个成员函数来释放线程
    aa[i].Terminate;
    aa[i].free;
      

  9.   

    那我想在这个线程类结束前的进候,做一些销毁工作,那是不是要:
    for i := 0 to 5 do
    begin
      a[i].FreeResource;
      a[i].terminate;
    end;这样子?或者别的方法?请指教!
      

  10.   

    给一个例子,一个form,两个Button,unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end; TMyThread = class(TThread)
      private
        { Private declarations }
      protected
        procedure Execute; override;
      public
        constructor Create(CreateSuspended: Boolean);
        destructor Destroy; override;//在这里做一些销毁工作
      end;
      
    var
      Form1: TForm1;
      thread: TMyThread;//全局变量implementation{$R *.dfm}{ TMyThread }constructor TMyThread.Create(CreateSuspended: Boolean);
    begin
      inherited Create(CreateSuspended);
      FreeOnTerminate := false; //设置为false
    end;destructor TMyThread.Destroy;
    begin
      //线程类结束前,在这里释放资源 
      MessageBox(0,'Destroy','in',1);
      inherited;
    end;procedure TMyThread.Execute;
    begin
      MessageBox(0,'execute','in',1);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      //创建线程button
      thread := TMyThread.Create(true);
      thread.Resume;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      //停止线程button
      thread.Terminate;
      thread.Free;
    end;end.
      

  11.   

    在Create时
      inherited Create(CreateSuspended);
      FreeOnTerminate := True; 
      

  12.   

    to do2008(事情做好了,才是事情)能不能给我一个在FreeOnTerminate为True的情况下的释放资源的例子啊?谢谢
      

  13.   

    在FreeOnTerminate为True的情况,也是在Destroy中释放资源 destructor TMyThread.Destroy;
    begin
      //线程类结束前,在这里释放资源 
      MessageBox(0,'Destroy','in',1);
      inherited;
    end;这是相同的.但是呢,FreeOnTerminate为True时,程序中不能调用terminate这个方法
    for i := 0 to 5 do
    begin
      //a[i].terminate; 这是不能调用
    end;因为,线程的execute函数执行完后,线程就会自动调用terminate来释放线程的资源
    即自动终止线程,不再显式地调用terminate方法来终止线程