正在学习线程,但却怎么了释放不了线程在关闭程序中写了却没有相着属性出现,强行写上也编译不过,全部的代码如下:
不知问题出在那里,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;TMyThread = class(TThread)
private
protected
procedure Execute(); override; //必須重寫,本方法即線程執行的方法,裡面調用事件,事件再調用方法
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);//啟動線程
var
aThread: TMyThread; //聲明線程
begin
aThread := TMyThread.Create(True); //線程實例化
aThread.Resume; //線程啟動
end;procedure TMyThread.Execute(); //線程啟動
var
IntLoop:integer;
begin
while not terminated do
begin
for intLoop:=1 to 100 do
begin
form1.memo1.lines.add(IntToStr(Intloop));
end; end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TMyThread.terminated;//在这里三个属性没有一个能显示出来,这里强行写上了,也编译不过去,不知问题出在那里
TMyThread.waitfor;
TMyThread.Free;
end;
end.
不知问题出在那里,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;TMyThread = class(TThread)
private
protected
procedure Execute(); override; //必須重寫,本方法即線程執行的方法,裡面調用事件,事件再調用方法
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);//啟動線程
var
aThread: TMyThread; //聲明線程
begin
aThread := TMyThread.Create(True); //線程實例化
aThread.Resume; //線程啟動
end;procedure TMyThread.Execute(); //線程啟動
var
IntLoop:integer;
begin
while not terminated do
begin
for intLoop:=1 to 100 do
begin
form1.memo1.lines.add(IntToStr(Intloop));
end; end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TMyThread.terminated;//在这里三个属性没有一个能显示出来,这里强行写上了,也编译不过去,不知问题出在那里
TMyThread.waitfor;
TMyThread.Free;
end;
end.
private
{ Private declarations }
public
释放的时候这样写
aThread.terminated
aThread.waitfor;
aThread.Free;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
aThread: TMyThread; //把声明放这里
public
{ Public declarations }
end; TMyThread = class(TThread)
private
protected
procedure Execute(); override; //必須重寫,本方法即線程執行的方法,裡面調用事件,事件再調用方法
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);//啟動線程
begin
aThread := TMyThread.Create(True); //線程實例化
aThread.Resume; //線程啟動
end; procedure TMyThread.Execute(); //線程啟動
var
IntLoop:integer;
begin
while not terminated do
begin
for intLoop:=1 to 100 do
begin
form1.memo1.lines.add(IntToStr(Intloop)); //用主线程的东西,你这里要同步,现在没有
end; end;
end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// TMyThread.terminated;//TMyThread是类,terminated不是类方法,你怎么可能这样调用?
aThread.terminated;
aThread.free;
end;
end. ==========================================
此外,还有一个更简单的方法你可以设它的FreeOnTerminate为True,这样持行完它会自己释放:
procedure TForm1.Button1Click(Sender: TObject);//啟動線程
var aThread :TMyThread;
begin
aThread := TMyThread.Create(True); //線程實例化
aThread.FreeOnTerminate:=True;//注意这里。
aThread.Resume; //線程啟動
end; 这样你就不用在Close里写代码了
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AThread.Terminated;//这里编译不过
AThread.WaitFor;
AThread.Free;
end;
改成下面的也编译不过,不知为何
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AThread.Terminated:=true;//这里也编译不过
AThread.WaitFor;
AThread.Free;
end;
begin
AThread.bStop:=true;
AThread.WaitFor;
AThread.Free;
end;
procedure TMyThread.Execute(); //線程啟動
var
IntLoop:integer;
begin
while not terminated and not bStop do
begin
for intLoop:=1 to 100 do
begin
form1.memo1.lines.add(IntToStr(Intloop)); //用主线程的东西,你这里要同步,现在没有
end; end;
end;
后面所个d
为了终止线程可以自己定义变量,用于标识要求终止线程,或使用线程消息。