实现线程Terminate方法的重写 [英] Implement override for thread Terminate method

查看:142
本文介绍了实现线程Terminate方法的重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖虚拟的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 the OnTerminate 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆