5

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.

Tom Hagen
  • 277
  • 2
  • 10
  • 1
    When converting to OTL, it pays to ask yourself - what is the bigger picture. What are you doing with this thread? Is this a one-shot background operation or are you scheduling TMyDBOp lots of times? If latter, does connection to the database stay the same and only executed commands change? If so, can connection stay open for long amounts of time? – gabr Oct 05 '12 at 07:46
  • @gabr For the purpose of this question consider it one-shot backgroud operation. I am aware of the messaging nature of OTL and already found that for repetitive tasks I coud make it TOmniWorker descant with Execute method converted into Message handler. – Tom Hagen Oct 05 '12 at 07:53
  • 1
    What's the motivation for changing? What problems do you have with the current implementation? – David Heffernan Oct 05 '12 at 07:55
  • @DavidHeffernan There is one thing that I really miss in my construct - "a communication with main VCL thread" (sending messages, progress etc.) = I really like the way that OTL implements communication (OmniTask.Comm.Send) and also general task controll in it. – Tom Hagen Oct 05 '12 at 08:14
  • @TomHagen then please tell us what you really want to do. I was going to answer your question with 'use Async', but it would become inappropriate if you want to use messaging. – gabr Oct 05 '12 at 08:15
  • Your edit doesn't add anything useful. What are your functional requirements? That's what @gabr is after. – David Heffernan Oct 05 '12 at 08:41
  • @DavidHeffernan I am sorry, I do not know what else should I add to the question to clarify. My intentions are that simple: How should I rewrite that TMyDBOp class to take advantage of OTL? – Tom Hagen Oct 05 '12 at 12:54
  • Just a couple of comments up you say that you want messages and progress. There's not of that in the question. What other requirements do you have? – David Heffernan Oct 05 '12 at 12:58

0 Answers0