如果我创建了一个线程
hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);
那这个线程什么时候结束呢?

解决方案 »

  1.   

    Execute 执行完
    或者外部终止它
      

  2.   

    有个api函数可以终止它的,但需要这个线程的句柄
      

  3.   

    外部终止线程 :  TerminateThread(TTheThread(TTestThread).Handle,0); //
      TTheThread(TTestThread).Free ;
      

  4.   

    啊,楼上的理解错了啊
     TerminateThread(hthread,0);这个api我知道,hthread可以通过
    hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);得到我不是说要怎么终结这个线程,而是问这个线程什么时候自动结束,
    返回个什么标志,参数之类的。而不要我手动终止。
    wjlsmail(计算机质子):Execute 操作是相对于Thread类吧,
     我没有用delphi封装的Thread。我只是调用api而已。
      

  5.   


    wjlsmail(计算机质子):TTheThread 是个什么东西?我怎么查不到帮助?
      

  6.   

    TTheThread :我自己定义的一个线程
      

  7.   

    TTheThread :我自己定义的一个线程类
      

  8.   

    楼上,我完全没有用到delphi封装的thread类。所以你的方法也不能实现我
    的目的。我目前是这样做的,我
    hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);
    创建一个线程来执行Dofunc函数,当这个函数执行完以后,
    给一个变量赋值,比如给TStatus:=True,然后搞个Timer不断的
    判断这个TStatus的状态,为True时就把这个线程咔嚓掉~这样做实在是有蛮龌龊。不知哪位老大有好的方法?
      

  9.   

    hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);结束 : DoFunc 执行完
      

  10.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      function MyThreadFunc(P : pointer) : Longint;stdcall;implementation{$R *.dfm}function MyThreadFunc(P : pointer) : Longint;stdcall;
    var
      i:integer;
      S:string;
    begin
      for i:=0 to 100000 do
        S:=Inttostr(i);
      MessageBox(Application.Handle,'执行完成','Note',Mb_Ok) ;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      HThread : THandle ;
      ThreadID : DWord ;
    begin
      HThread := CreateThread(Nil,0,@MyThreadFunc,Nil,0,ThreadID) ;
    end;end.
      

  11.   

    在线程内部用ExitThread(exitcode);
    外部用GetExitCodeThread(hthread)来取得exitcode
      

  12.   

    一个线程的结束有二种方式:
    自动退出:
         是指在线程完成其Execute方法后直接返回,如果此时线程对象的FreeOnTerminate属性设为True,则线程将自动删除,并释放线程对象所占用的资源;受控退出:
         由进程或其他线程调用所需结束的线程对象的Terminate方法,并设置线程对象的Terminate属性为True.