PatV 1 Posted September 24, 2019 (edited) 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 September 24, 2019 by PatV add delphi version and Omnithread version Share this post Link to post
Primož Gabrijelčič 223 Posted September 25, 2019 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
PatV 1 Posted September 25, 2019 (edited) Thanks again Primož, So glad you are around. Patrick Edited September 25, 2019 by PatV change size of text Share this post Link to post
Primož Gabrijelčič 223 Posted September 25, 2019 Yeah, as it turned out, SetThreadDataFactory doesn't yet support an anonymous method factory. Have to fix that ... 😞 I'll write a better answer soon (today). Share this post Link to post
Primož Gabrijelčič 223 Posted September 25, 2019 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