How to replace TThread with OmniThreadLibrary?

713 Views Asked by At

I am used to create TThread descendants to do longlasting DB operations. My usual construct looks like this:

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;

Then I obviously use this class with

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;

And of course I usually set DBOp as another component var, to be able to Terminate, or WaitFor the thread.

Now I would like to rewrite these TThread classes and use simillar construct with OmniThreadLibrary. How should i do that? I mean: What base class to use to define all the class components and properites? - Should it be a TOmniWorker descendant? Then where is the Execute procedure? - Should it be a TObject descendand and the OTLTaks is then created as CreateTask(DBOp.Execute)? - Should it be a TObject that I pass as a Parameter for OTLTask created as CreateTask(anonymous method that reads the parameter and calls its Execute)?

Thanks for any hint.

EDIT: (based on gabrs' comment on clarification) My point is that all the samples/tests in OTL source show only a simple usage. Mostly basic "one-procedure" threads. For my case I need a rather complex class with subcomponents and subroutines that is all run in a thread. I am look for such a class ancestor and its design pattern.

0

There are 0 best solutions below