Search code examples
delphiomnithreadlibrary

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!


Solution

  • 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.