你测试一下看看有没有错:)------------------------------ unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,UThTest;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure a(Sender : TObject) ; private { Private declarations } public { Public declarations } end;var Form1: TForm1; tt : TTest ;implementation{$R *.dfm}procedure TForm1.a(Sender : TObject) ; begin Caption := 'End Of The Thread' ; end ;procedure TForm1.Button1Click(Sender: TObject); begin tt := TTest.Create(False); tt.OnTerminate := a ; end;procedure TForm1.Button2Click(Sender: TObject); begin TerminateThread(TTest(tt).Handle,0); TTest(tt).Free ; Caption := 'end ' ; end;end.---------------------------- unit UThTest;interfaceuses windows, Classes, SysUtils;type TTest = class(TThread) private { Private declarations } protected procedure Execute; override; end;implementation{ Important: Methods and properties of objects in VCL or CLX can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TTest.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ TTest }uses Unit1 ;procedure TTest.Execute; var i : Integer ; begin { Place thread code here } for i := 0 to 10 do begin Sleep(1000) ; end ; end;end.
tt : TTest ; TerminateThread(TTest(tt).Handle,0);
TTest(tt).Free ;
exitcode:dword;
p:cardinal;
...
GetExitCodeThread(thread.handle,p);
exitcode:=dword(pointer(p)^);
Terminatethread(thread.handle,exitcode);2000/NT下,该线程需要有THREAD_QUERY_INFORMATION、THREAD_TERMINATE 访问权
如何设置THREAD_QUERY_INFORMATION、THREAD_TERMINATE 等的权限
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,UThTest;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;
tt : TTest ;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); // 创建线程
begin
tt := TTest.Create(False);
end;procedure TForm1.Button2Click(Sender: TObject); // 终止线程
begin
TerminateThread(TTest(tt).Handle,0);
TTest(tt).Free ;
Caption := 'end ' ;
end;end.-------------------------unit UThTest;interfaceuses
windows,
Classes;type
TTest = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;implementationuses Unit1 ;procedure TTest.Execute;
begin
{ Place thread code here }
while not Terminated do
begin
Sleep(10000) ;
end ;
end;end.
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD这个参数
DWORD dwStackSize, // initial stack size
LPTHREAD_START_ROUTINE lpStartAddress, // thread function
LPVOID lpParameter, // thread argument
DWORD dwCreationFlags, // creation option
LPDWORD lpThreadId // thread identifier
);
还是不行阿,我的线程操作后台数据库,异常中断难道非得报错吗
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,UThTest;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure a(Sender : TObject) ;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
tt : TTest ;implementation{$R *.dfm}procedure TForm1.a(Sender : TObject) ;
begin
Caption := 'End Of The Thread' ;
end ;procedure TForm1.Button1Click(Sender: TObject);
begin
tt := TTest.Create(False);
tt.OnTerminate := a ;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
TerminateThread(TTest(tt).Handle,0);
TTest(tt).Free ;
Caption := 'end ' ;
end;end.----------------------------
unit UThTest;interfaceuses
windows,
Classes,
SysUtils;type
TTest = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;implementation{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TTest.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TTest }uses Unit1 ;procedure TTest.Execute;
var
i : Integer ;
begin
{ Place thread code here }
for i := 0 to 10 do
begin
Sleep(1000) ;
end ;
end;end.