5

我习惯于创建 TThread 后代来执行持久的数据库操作。我通常的构造如下所示:

interface
type
  TMyDBOp = class(TThread)
  private
    FConnection: TADOConnection;
    FCommand1: TADOCommand;
    FCommand2: TADOCommand; // and many more privated objects, vars ...
    procedure InitDB;
    procedure DoneDB;
    procedure DoDBStuff; // and many more procedures and functions to structure the code
  protected
    procedure Execute; override;
  public
    constructor Create; reintroduce;
    property X: T... // and many more properties to set on thread creation
  end;

implementation

constructor TMyDBOp.Create;
begin
  inherited Create(False);
end;

procedure TMyDBOp.InitDB;
begin
  FConnection:= TADOConnection.Create(nil);
  // setup all connection props, setup all other components
end;

procedure TMyDBOp.DoneDb;
begin
  FConnection.Close; // and Free, and Free all other components
end;

procedure TMyDBOp.DoDBStuff;
begin
  // Execute FCommands, do some calculations, call other TMyDBOp methods etc. etc.
end;

procedure TMyDBOp.Execute;
begin
  Try
    Coinitialize;
    InitDB;
    try
      DoDBStuff;
    finally
      DoneDB;
    End;
  Except
    // catch, log, report exceptions ....
  End;
end;

然后我显然使用这个类

var
  DBOp: TMyDBOp;
begin
  DBOp:= TMyDBOp.Create;
  DBOp.Property1:= xyz; // and set all other props
  DBOp.OnTerminate:= DBOpTerminated; // procedure to catch the thread and read back the results etc. etc.
  DBOp.Resume;
end;

当然,我通常将 DBOp 设置为另一个组件 var,以便能够终止或 WaitFor 线程。

现在我想重写这些 TThread 类并使用与 OmniThreadLibrary 类似的构造。我该怎么做?我的意思是:使用什么基类来定义所有类组件和属性?- 应该是 TOmniWorker 的后代吗?那么 Execute 程序在哪里呢?- 它应该是 TObject 的后代,然后将 OTLTaks 创建为CreateTask(DBOp.Execute)?- 它应该是我作为参数传递给 OTLTask 创建的 TObjectCreateTask(anonymous method that reads the parameter and calls its Execute)吗?

感谢您的任何提示。

编辑:(基于 gabrs 对澄清的评论)我的观点是,OTL 源代码中的所有样本/测试都只显示了一个简单的用法。主要是基本的“单一过程”线程。对于我的情况,我需要一个相当复杂的类,其中包含所有在线程中运行的子组件和子例程。我正在寻找这样的类祖先及其设计模式。

4

0 回答 0