实现线程Terminate方法的重写 [英] Implement override for thread Terminate method
问题描述
我试图覆盖虚拟的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属性之前调用方法,则必须重写rel ="nofollow"> System.Classes.TThread.Terminate 方法.
Terminate
方法:
The System.Classes.TThread.Terminate method has to be overridden if you want a method to be called before the Terminate
property is set.
The Terminate
method:
通过将
Terminated
属性设置为true来向线程发出信号以终止线程.
Signals the thread to terminate by setting the
Terminated
property to true.
从 System.Classes.TThread.DoTerminate 文档:
DoTerminate
调用OnTerminate
事件处理程序,但不终止线程.
DoTerminate
calls theOnTerminate
event handler, but does not terminate the thread.
在OnTerminate
事件之前但在Terminate
属性设置为True
之后调用的另一个感兴趣的过程是
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 abstract
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方法的重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!