对不起,对不起。看下面吧:
========================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TQueryThread = class(TThread)
protected
procedure Execute;
override;
procedure Updata;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Stop:Boolean;
QT:TQueryThread;
QT_Handle:THandle;
ExitCode
Word;
procedure TerminateIt(Sender: TObject);
procedure ThreadKill;
procedure ThreadStart;
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ThreadStart;
begin
Stop:=True;
qt:=TQueryThread.Create(True);
QT_Handle:=qt.Handle;
with qtdo
begin
FreeOnTerminate:=True;
OnTerminate:=TerminateIt;
// 测试时可以用这个过程查看
Resume;
end;
end;
procedure TQueryThread.Updata;
begin
// 画 Form 的颜色
if Form1.Color=clBlack then
Form1.Color:=clBtnFace
else
Form1.Color:=clBlack;
end;
procedure TForm1.TerminateIt;
begin
// 自然终止的话,显示消息
MessageBox(0,'线程终止了','信息',MB_OK);
end;
// 强行终止线程
procedure TForm1.ThreadKill;
begin
TerminateThread(QT_Handle,ExitCode);
end;
procedure TQueryThread.Execute;
begin
while Form1.Stopdo
begin
// 这里执行一个查询,在发出查询后,等待返回 (实际上就是休眠)
Synchronize(Updata);
// 随便执行一个死循环,这里是画 Form 颜色
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// 线程启动
ThreadStart;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// 自然终止线程
Stop:=false;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
// 强行终止线程
ThreadKill;
end;
end.
======================================================================
以后我一定仔细检查自己写的代码
,呵呵。