[英]Terminating a thread in Delphi 10 with Android
I'm creating a thread with the basic settings indicated below. 我正在使用以下指示的基本设置创建一个线程。 Apparently everything is working fine, but when I run
MyThread.Terminate
I noticed that Destroy
does not run. 显然,一切正常,但是当我运行
MyThread.Terminate
我注意到Destroy
无法运行。 Destroy
is only executed when closing the main application. Destroy
仅在关闭主应用程序时执行。
What is the correct way to end a thread? 结束线程的正确方法是什么?
BASIC THREAD 基本线程
// MyThread "TPoolingThread"
constructor TPoolingThread.Create;
begin
inherited Create(True);
// Initializing something
end;
destructor TPoolingThread.Destroy;
begin
inherited Destroy;
end;
procedure TPoolingThread.Execute;
begin
try
while (not Terminated) do begin
sleep(100);
// do something
end;
finally
if not Terminated then
Terminate;
end;
end;
EXECUTING THREAD 执行线程
// Main Application
//////////////////////////////////////////
// Create and Start
MyThread := TPoolingThread.Create;
if Assigned(MyThread.FatalException) then
raise MyThread.FatalException;
MyThread.Start;
//////////////////////////////////////////
// Stop
MyThread.Terminate;
I'd like to preface my answer by defining 'ending a thread' . 我想通过定义“结束线程”来开头我的回答。 I'm going to assume that by 'ending a thread' you mean to stop your loop in the
TPoolingThread.Execute
procedure. 我将假设通过“结束线程”意味着停止
TPoolingThread.Execute
过程中的循环。
Terminating the thread 终止线程
The method you use to terminate your thread is perfectly acceptable. 用于终止线程的方法是完全可以接受的。 Now as to why it is, allow me to show you this snippet :
现在,为什么,请允许我向您展示此片段 :
Version 1
版本1
In a Try-Finally construct, the Finally statement is guaranteed to be executed absolutely regardless of what happens in the try clause.
在Try-Finally构造中,无论try子句中发生了什么,都保证可以绝对执行Final语句。 However, the Finally clause does not actually handle any exceptions - the program will terminate if no Except clause is found (see notes below).
但是,Finally子句实际上并不处理任何异常-如果未找到Except子句,则程序将终止(请参见下面的注释)。
Therefore if your code within your loop throws an exception, you tell your TPoolingThread
object that it has been terminated. 因此,如果循环中的代码引发异常,则告诉
TPoolingThread
对象它已终止。
Why destroy isn't getting called 为什么销毁没有被召唤
As Remy Lebeau and Dalija Prasnikar suggest. 正如雷米·勒博(Remy Lebeau)和达莉亚·普拉斯尼卡(Dalija Prasnikar)所说 。 Terminate is not supposed to call destroy:
终止不应该称为销毁:
System.Classes.TThread.Terminate:
System.Classes.TThread.Terminate:
Signals the thread to terminate by setting the Terminated property to true.
通过将Terminated属性设置为true来指示线程终止。 Terminate sets the thread's Terminated property to true, signaling that the thread should be terminated as soon as possible.
Terminate将线程的Terminated属性设置为true,表示应尽快终止线程。
For Terminate to work, the thread's Execute method and any methods that Execute calls should check Terminated periodically and exit when it's true.
为了使Terminate起作用,线程的Execute方法和Execute调用的任何方法都应定期检查Terminated并在其为true时退出。
I will leave you this link if you want to find out more about ARC in Delphi for Android development. 如果您想了解有关Delphi中用于Android开发的ARC的更多信息,我将保留此链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.