Delphi VCL/RTL 有类似 AutoThread 类的东西吗?即,类的每个方法都派生自例如然后,TAutoThread 类将在单独的线程中自动执行,而无需编写任何线程特定的代码。
最佳答案
使用Anonymous thread
可以制作类似于 AutoThread 类的东西。
只需传递一个匿名过程并调用线程即可。
var
aThread : TThread;
...
aThread :=
TThread.CreateAnonymousThread(
procedure
begin
// your code to execute in a separate thread here.
end
);
aThread.Start; // start thread and it will execute and self terminate
注意,这与从另一个类派生的类无关,但结果是相似的。您不必编写任何线程特定的代码。当然,您必须遵循正常的线程规则。
<小时/>如果您需要在线程完成时收到通知,请在启动线程之前定义一个 OnTerminate
方法。它将在主线程中执行。
aThread.OnTerminate := Self.ThreadFinishedNotification;
关于multithreading - Delphi VCL 中的 AutoThread 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19069546/