Jump to content
PatV

Passing parameter to ThreadDataFactory

Recommended Posts

Delphi 10.3

Omnithead 3.07.5

 

Hi All,

 

I'm just a little lost, I would like to pass parameters to the data factory, I know I can pass function like this

 

TOTPThreadDataFactoryFunction = function: IInterface;
TOTPThreadDataFactoryMethod = function: IInterface of object;

 

and my function is like 

 

{-------------------------------------------------------------------------------------------------}

 

function TFConfig.CreateThreadPool(aHandle : THandle ; aConfig : rConnectionConfig ;  aValue: string) : iOmniThreadPool;
var
 iOmniValue : TOmniValue;
begin
  iOmniValue := TOmniValue.FromRecord<rConnectionConfig>(aconfig);

  result  := otlThreadPool.CreateThreadPool(aValue);
  result.MaxExecuting := result.NumCores;

 

  result.SetThreadDataFactory
  (
    (function (aHandle : THandle ; aConfig : TOmniValue) : IInterface
     begin
       result:=Function : IInterface
               begin
                  result:=  ??
               end;
     end
    )(aHandle,iOmniValue
)
  );

end;

 

{-------------------------------------------------------------------------------------------------}

 

rConnectionConfig is a record containing info on to be able to connect to the datamodule

 

  RConnectionConfig = record
    User            : string;
    Password        : string;
    Database        : string;
    Protocol        : string;
    Port            : integer;
    HostName        : string;
    LibraryLocation : string;
    ModuleName      : string;
  end;

 

Could someone help me with this

 

Thanks

 

Patrick

 

 

 

 

 

 

 

 

 

Edited by PatV
add delphi version and Omnithread version

Share this post


Link to post

You can just use aConfig inside the anonymous function. Compiler will capture it for you.

 

function TFConfig.CreateThreadPool(aHandle : THandle ; aConfig : rConnectionConfig ;  aValue: string) : iOmniThreadPool;
begin
  result := otlThreadPool.CreateThreadPool(aValue);
  result.MaxExecuting := result.NumCores;

  result.SetThreadDataFactory
  (
    function: IInterface
     begin
       result:= MakeInterface(aHandle, aConfig); // implement MakeInterface 
     end
  );
end;

 

Share this post


Link to post

Thanks again Primož, 

 

So glad you are around.

 

Patrick

Edited by PatV
change size of text

Share this post


Link to post

OK, now for reals 🙂 

 

(Sorry for the wrong answer before. You did not provide a test project so I did not open my Delphi at all and just guessed at the answer.)

 

As the SetThreadDataFactory doesn't yet support anonymous method factory, your best bet is to use a singleton to store parameters and provide a factory.

 

Just a sketch of a solution:

 

type
  TThreadFactory = class
  public
    class var Handle: THandle;
    class function Make: IInterface;
  end;

  FConnectionPool := CreateThreadPool('Connection pool');
  TThreadFactory.Handle := aHandle;
  FConnectionPool.SetThreadDataFactory(TThreadFactory.Make);

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×