multithreading - Delphi VCL 中的 AutoThread 类?

标签 multithreading delphi

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/

相关文章:

java - 创建 n 个线程

multithreading - 每个核心的多个线程中存在的并行度

java - 如何让这个java程序在多核上运行?

swift - Swift中修改引用类型参数;外面的变化是立即可见的吗?

delphi - 使用 TIdNotify.NotifyMethod 时某些数据包被丢弃

delphi - 类似于 System Internal 的/Microsoft 的 FileMon/Process Monitor 的监视文件

linux - 是否有与 Windows 手动重置事件等效的 UNIX/pthreads?

delphi - 扩展delphi TEditCopy操作以将列表框项目也复制到剪贴板

Delphi - 如何检测防病毒软件的存在?

delphi - 在Delphi匿名方法中不能使用var参数吗?