TerminateThread一共才两个参数有什么难的,怎么会不会?
只要有线程句柄最为第一个参数就可以了

解决方案 »

  1.   

    直接用TerminateThread来结束线程不安全吧?
      

  2.   

    Tmythread=class(tthread)
    th:Tmythread;procedure TForm1.Button1Click(Sender: TObject);//创建线程th
    begin
    th:=tmythread.create(flase);
    th.freeonterminate:=true;
    end;procedure TForm1.Button2Click(Sender: TObject);//停止线程th
    begin
    if not th.Suspended then
    begin
     th.Suspend;
     th.Terminate;
    end;
    end;我试过,可以的
      

  3.   

    To knock(农民):
    在procedure TForm1.Button2Click(Sender: TObject);中
    可以引用th吗?为什么我的会说未定义`
      

  4.   

    to szkxy(雨鱼) :
    可以引用啊,你把
    var th:Tmythread;
    这句中把 th 定义为全局变量就可以了;我昨天刚试过,没问题,
    如果 th 定义在TForm1.Button1Click过程内,就是局部变量,
    TForm1.Button2Click过程中当然不能使用别的过程的局部变量
      

  5.   

    TerminateThread不太安全
    设置Terminated会好一点
      

  6.   

    to: peitsiang_gimi() 
    直接用terminate会出现一些异常情况,像本例就可能按下button2后不能终止线程!
      

  7.   

    干脆把代码贴出来,这是个很简单的线程,,有什么问题可以和我讨论讨论:[email protected]主程序:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls,unit2;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
         Form1:TForm1;
       thread1:Tmythread;
    implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
    thread1:=tmythread.create(false);
      sleep(1000);
     thread1.freeonTerminate:=true;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    if not thread1.Suspended then
    begin
     thread1.Suspend;
     thread1.Terminate;
    end;
    end;end.**************************************************************
    unit Unit2;interfaceuses
      Classes,Sysutils;type
      tmythread = class(TThread)
      private
        { Private declarations }
      protected
        procedure Execute; override;
      end;implementation
      uses unit1;
    { Important: Methods and properties of objects in VCL 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; }{ tmythread }procedure tmythread.Execute;
    var i:integer;
    begin
      for i:=0 to 10000  do
      form1.caption:=inttostr(i); //(只有一个子线程)
    end;end.