Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass params to Parallel.Async() in OmniThreadLibrary?

How does one pass some parameters from the main thread to the working thread (ie the following procedure)?

Parallel.Async(
  procedure (const task: IOmniTask)
  begin
    //How does one pass in 'myParam' from the main thread, so that the statement bellow will work?
    s := task.Param['myParam'].AsString;
  end
);

If you check the definition of IOmniTaskConfig (in OtlParallel.pas), there is a commented out property called Param, like the following:

//    property Param: TOmniValueContainer read GetParam;

So I guess the answer to my question is no, but I wish it's not!

like image 266
Edwin Yip Avatar asked Dec 30 '25 12:12

Edwin Yip


1 Answers

You are expected to use variable capture for this.

var
  MyParam: Integer;
....
MyParam := 42;
Parallel.Async(
  procedure(const task: IOmniTask)
  begin
    Foo(MyParam);
  end
);

In case you are not familiar with variable capture for anonymous methods, it's discussed in some detail in the documentation.

like image 66
David Heffernan Avatar answered Jan 02 '26 03:01

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!