TYPE
  TTestThread = class(TThread)
  private
  protected
    procedure Execute; override;
  end;procedure TTestThread.Execute;
begin
  FreeOnTerminate := True;
  //线程内容
end;我用TTestThread.Create(False);启动线程,可用TTestThread(0).Terminate;终止线程就报错,用TerminateThread(TestThread.Handle,0);语法检查都通不过。

解决方案 »

  1.   

    maybe you need this;
    when you  need a thread like your ttestthread,you can like;
    var
      tmpthread:TTestThread;
    begin
      tmpthread:=TTestTheead.Create(false);
      //terminated a thread like
      tmpthread.terminate;
    end;
      

  2.   

    我在用线程播放MIDI,终止时还是在播放
    MediaPlayer1.Close;tmpthread.terminate;
      

  3.   

    關鍵看你 Execute 中寫了什麼代碼, 還有, 如你FreeOnTerminate := True;執行完已經釋放, 你再終止, 也會錯, 還有, 你只不過聲明了一個類而已, 運行, 還要聲明創建實例!建議你找本書看看!
      

  4.   

    unit Unitprocess;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,TlHelp32, ExtCtrls, Spin;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
    begin
      close;
    end;
    procedure  ProcessRefesh();
    var lppe: TProcessEntry32;
        ssHandle: THandle;
        Wnd: HWND;
        AppFound: Boolean;
    begin
      ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
      AppFound := Process32First(sshandle, lppe);
      while AppFound do
      begin
        //其中lppe.szExefile就是程序名**********************************************
        form1.ListBox1.Items.Add (UpperCase(lppe.szExeFile)) ;
      //  if (UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName)) then
      //    begin
     //     Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
     //     TerminateProcess(Wnd, 0);
     //   end;
        AppFound := Process32Next(ssHandle, lppe);
      end;
    end;
    procedure  ProcessExit(AppName:string);
    var lppe: TProcessEntry32;
        ssHandle: THandle;
        Wnd: HWND;
        AppFound: Boolean;
    begin
      ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
      AppFound := Process32First(sshandle, lppe);
      while AppFound do
      begin
        //其中lppe.szExefile就是程序名**********************************************
       // form1.ListBox1.Items.Add (UpperCase(lppe.szExeFile)) ;
       if (UpperCase((lppe.szExeFile))=UpperCase(AppName)) then
         begin
         Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
         TerminateProcess(Wnd, 0);
        end;
        AppFound := Process32Next(ssHandle, lppe);
      end;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
       listbox1.clear;
       ProcessRefesh;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
       listbox1.clear;
       ProcessRefesh;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var str:string;
        i:integer;
    begin
        if listbox1.SelCount>0 then
        begin
           for i:=0 to listbox1.ItemHeight do
           begin
              if listbox1.Selected [i] then
              begin
               str:=listbox1.Items[i];
               form1.Caption:=str;
              end;
               ProcessExit(str);
           end;
        end;
    end;end.