读书人

以下是小弟我的一段代码怎么随时终止

发布时间: 2012-02-09 18:22:27 作者: rapoo

以下是我的一段代码,如何随时终止线程?
我想实现在任何时候终止n线程,但是用以下方法没反映,反而程序死了.


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
n:Tthread;

implementation

uses unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin
n:=Ttestthread.create(false);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
n.Terminate;
n.Free;
end;

end.


unit Unit2;

interface

uses
Classes,SysUtils;

type
TtestThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

uses unit1;

{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TtestThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread ';
end; }

{ TtestThread }

procedure TtestThread.Execute;
var i,t:integer;
begin
FreeOnTerminate := true;
t:=1;
while t=1 do
begin
for i:=1 to 20000 do
begin
form1.label1.Caption:=inttostr(i);
end;
end;
end;

end.


[解决办法]
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }


end;

TtestThread = class(TThread)
private
FIndex: integer;
procedure WriteCaption;
protected
procedure Execute; override;
end;

var
Form1: TForm1;
myThread: TTestThread;

implementation

{$R *.dfm}

{ TtestThread }


procedure TtestThread.Execute;
begin
FreeOnTerminate := true;
while not Terminated do
begin
Inc(FIndex);
if FINdex > 20000 then FIndex := 1;
Synchronize(Self, WriteCaption);
Sleep(10);
end;
end;


procedure TtestThread.WriteCaption;
begin
form1.label1.Caption:= inttostr(FIndex);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if myThread = nil then
begin
MyThread := TTestThread.Create(True);
MyThread.FIndex := 1;
MyThread.Resume;
end
else begin
MyThread.Terminate;
MyThread := nil;
end;

end;

end.

按一下button1开始, 再按一下结束。

你的问题在于

while t=1 do //没有结束条件,相当于 While True do了,死循环
begin
for i:=1 to 20000 do //同样,只要到了这就最起码得来20000次
begin
form1.label1.Caption:=inttostr(i);
end;
end;
end;

[解决办法]
或者设置标志看是否结束线程
type
TtestThread = class(TThread)
private
runable:boolean;
{ Private declarations }
protected
procedure Execute; override;
end;

while true and runable do
begin
for i:=1 to 20000 do //同样,只要到了这就最起码得来20000次
begin
if runable then
form1.label1.Caption:=inttostr(i)
else
break;
end;
end;
end;


读书人网 >.NET

热点推荐