• 主页
  • 招聘
  • 语言基础/算法/系统设计
  • 数据库相关
  • 图形处理/多媒体
  • 网络通信/分布式开发
  • VCL组件开发及应用
  • Windows SDK/API
wintergoes 河北科技大学 高级软件开发工程师  2009年03月04日
为什么线程执行Free后,还能调用它的Terminated属性?
线程执行Free后内存不是已经释放掉了吗?为什么还能调用它的Terminated属性,还可以调用其他的一些自定义属性.

unit Unit1;

interface

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

type
TTestThread = class(TThread)
private
procedure DrawLine;
procedure Execute; override;
public
FStr: string;
destructor Destroy; override;
end;

type
TForm1 = class(TForm)
imgTest: TImage;
Button1: TButton;
Button2: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
edtFreeTest: TEdit;
lstThreadHandle: TListBox;
Button8: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private
FTestThread: TTestThread;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TTestThread }

destructor TTestThread.Destroy;
begin
FStr := 'Is Destroyed';
inherited;
end;

procedure TTestThread.DrawLine;
var
I, J: Integer;
begin
Form1.imgTest.Canvas.FillRect(Form1.imgTest.Canvas.ClipRect);
for I := 0 to Form1.imgTest.Width do
begin
for J := 0 to Form1.imgTest.Height do
begin
Application.ProcessMessages ;
Randomize;
Form1.imgTest.Canvas.Pixels[I, J] := RGB(Random(255), Random(255), Random(255));
end;
end;
end;

procedure TTestThread.Execute;

begin
inherited;
while not Terminated do
begin
Synchronize(DrawLine);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
FTestThread := TTestThread.Create(True);
FTestThread.FreeOnTerminate := True;
FTestThread.FStr := 'Runing';
FTestThread.Resume;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
FTestThread.Terminate;
end;



procedure TForm1.Button4Click(Sender: TObject);
begin
if Assigned(FTestThread) then
ShowMessage('Thread is not nil')
else
ShowMessage('Thread is nil')
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
if FTestThread.Terminated then
ShowMessage('Thread is Terminate')
else
ShowMessage('Thread is not Terminate')
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
ShowMessage(FTestThread.FStr);
end;



procedure TForm1.Button8Click(Sender: TObject);
begin
lstThreadHandle.Items.Add(IntToStr(Integer(@FTestThread.Handle)));
end;


end.
...全文
332 点赞 收藏 19
写回复
19 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Delphi
创建于2007-08-02

1461

社区成员

26.2w+

社区内容

Delphi 开发及应用
社区公告
暂无公告