一个关于线程的小问题(100分)

  • 主题发起人 主题发起人 dongliang_110
  • 开始时间 开始时间
D

dongliang_110

Unregistered / Unconfirmed
GUEST, unregistred user!
我看到书上有一段线程的例子,是这样的:
procedure Ttestthread.execute;
var i:integer;
begin
freeonterminated:=true;
for i:=0 to 20000do
begin
if terminated then
break;
inc(i)
end;
end;

我看了书上其他部分的代码,但是我没有发现将terminated属性设置成true是什么时候。请问
这个属性是在什么设置的。
 
是在调用线程后,想结束线程时,
ttestthread.terminate时,terminated属性就成了true了.
[:D]
 
The thread's Execute method and any methods that Execute calls should check
Terminated periodically and exit when it's True. [red]The Terminate method sets the
Terminated property to True.[/red]

The Terminate method is the polite way to abort the execution of a thread, but
it requires cooperation from the thread抯 Execute code. Using Terminate is
recommended over the TerminateThread Win32 API call.
 
Indicates whether the thread has been asked to terminate.
property Terminated: Boolean;
Description
The thread's Execute method and any methods that Execute calls should check Terminated periodically and exit when it's True.. The Terminate method sets the Terminated property to True.
 
他们说的都非常正确[:D][:D]
 
'大家已经说了
 
大家都说了,我就举个例子:
WINDOWS中拷贝文件的时候,显示一个“正在复制”窗体,拷贝在线程中执行:
procedure TCopyThread.execute;
var i:integer;
begin
freeonterminated:=true;
for i:=0 to 20000do
begin
if terminated then
break;
CopyFile('文件i或文件的第i块');
inc(i);
end;
end;

当我们点击“取消”时,执行:
procedure BtnCancelClick(Sender:TObject);
begin
CopyThread.Terminate;
end;
这时线程并不一定马上停止,而是由线程中检测到停止标识主动退出。
if terminated then
break;
 
后退
顶部