问题描述
我试图覆盖虚拟的TThread :: Terminate方法,但是发现我的覆盖仅在设置了Terminated标志后 被调用.实现此目的的正确方法是什么?
I am trying to override the virtual TThread::Terminate method, but am finding my override gets called only after the Terminated flag is set. What is the correct way to implement this?
TTestThrd = class(TThread)
private
{ Private declarations }
protected
{ Protected declarations }
procedure Execute(); override;
procedure DoTerminate(); override;
public
{ Public declarations }
end;
procedure TTestThrd.Execute();
var
bTerminated: Boolean;
begin
// This is the main thread loop
try
while (not Terminated) do
begin
// Sleep or use TEvent::Waitfor...
end;
finally
// Terminated (or exception) so free all resources...
bTerminated := True; // Why is this called...
end;
end;
procedure TTestThrd.DoTerminate();
var
bDoTerminateCalled: Boolean;
begin
bDoTerminateCalled := True; // ...before this?
inherited;
end;
推荐答案
Terminate
方法:
The Terminate
method:
从 System.Classes.TThread.DoTerminate 文档:
在OnTerminate
事件之前但在Terminate
属性设置为True
之后调用的另一个感兴趣的过程是 System.Classes.TThread.TerminatedSet ,它在TThread
类中声明为 virtual
.
Another procedure of interest that is called before the OnTerminate
event but after the Terminate
property is set to True
is System.Classes.TThread.TerminatedSet which is declared as virtual
in the TThread
class.
我现在没有IDE,但是我想如果您查看TThread
类,则会发现OnTerminate
事件处理程序是在Terminate
属性设置为True
之后触发的
I have no IDE with me right now but I guess that if you look in the TThread
class, you'll find that the OnTerminate
event handler is triggered after the Terminate
property is set to True
.
这篇关于实现线程Terminate方法的重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!