实现线程Terminate方法的重写

实现线程Terminate方法的重写

本文介绍了实现线程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方法:


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方法的重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:53