给你一个例子
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyThread = class(TThread)
public
procedure Execute;
override;
end;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Stoped: Boolean;
CurPos: Integer;
implementation
{$R *.dfm}
{$R WindowsXP.res}
procedure TForm1.Button1Click(Sender: TObject);
var
MyThread: TMyThread;
begin
MyThread := TMyThread.Create(False);
MyThread.FreeOnTerminate := True;
end;
{ TMyThread }
procedure TMyThread.Execute;
var
I: Integer;
begin
inherited;
Stoped := False;
for I := CurPos to 1000000do
begin
if Stoped then
begin
CurPos := I;
Exit;
end;
Form1.Edit1.Text := IntToStr(I);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Stoped := True;
end;
end.