unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unit2, ComCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
pb2: TProgressBar;
pb1: TProgressBar;
Button3: TButton; procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
pb1Thread:TMyThread;
pb2Thread:TMyThread;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
pb1Thread:=TMyThread.Create(true);
pb1Thread.FreeOnTerminate:=true;
pb1Thread.pbID:=1;
pb1Thread.Resume;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
pb1Thread:=TMyThread.Create(true);
pb1Thread.FreeOnTerminate:=true;
pb1Thread.pbID:=2;
pb1Thread.Resume;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
pb1Thread.Terminate;
end;end.
/////////////////////////////////////////////////////////////
线程单元:
unit Unit2;interfaceuses
Classes;type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure StepProgress;
public
pbID:1..2;
end;implementation
uses Unit1;
{ 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 TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TMyThread }procedure TMyThread.Execute;
var
i:cardinal;
begin
i:=1;
while (i<100000) do
begin
synchronize(stepprogress);
Inc(i);
end;
end;procedure TMyThread.StepProgress;
begin
if pbID=1 then Form1.pb1.StepIt
else Form1.pb2.StepIt;
end;end运行的时候,我先点击button1,启动pb1Thread线程,然后点击button3,企图终止pb1Thread,但是程序似乎没有理我,滚动条依然闪个不停,怎么回事?
希望能得到你的帮助!
谢谢!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unit2, ComCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
pb2: TProgressBar;
pb1: TProgressBar;
Button3: TButton; procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
pb1Thread:TMyThread;
pb2Thread:TMyThread;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
pb1Thread:=TMyThread.Create(true);
pb1Thread.FreeOnTerminate:=true;
pb1Thread.pbID:=1;
pb1Thread.Resume;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
pb1Thread:=TMyThread.Create(true);
pb1Thread.FreeOnTerminate:=true;
pb1Thread.pbID:=2;
pb1Thread.Resume;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
pb1Thread.Terminate;
end;end.
/////////////////////////////////////////////////////////////
线程单元:
unit Unit2;interfaceuses
Classes;type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure StepProgress;
public
pbID:1..2;
end;implementation
uses Unit1;
{ 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 TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TMyThread }procedure TMyThread.Execute;
var
i:cardinal;
begin
i:=1;
while (i<100000) do
begin
synchronize(stepprogress);
Inc(i);
end;
end;procedure TMyThread.StepProgress;
begin
if pbID=1 then Form1.pb1.StepIt
else Form1.pb2.StepIt;
end;end运行的时候,我先点击button1,启动pb1Thread线程,然后点击button3,企图终止pb1Thread,但是程序似乎没有理我,滚动条依然闪个不停,怎么回事?
希望能得到你的帮助!
谢谢!
var
i:cardinal;
begin
i:=1;
while (i<100000) do
begin
synchronize(stepprogress);
Inc(i);
if Terminated then Exit; //<<--
end;
end;
var
i:cardinal;
begin
i:=1;
while (i<100000) do
begin
if self.Terminated then
Break;
synchronize(stepprogress);
Inc(i);
end;
end;