My Short Answer
Parallel.Async is intended to be used with an anonymous method, and doing so allows you to capture variables from the calling method (as already demonstrated by David Heffernan).
My Long Answer (since you specifically asked about the Param collection)
The task.Param
from your snippet doesn't refer to the property in the IOmniTaskConfig
interface. It refers to the Param property in the IOmniTaskControl
interface. It is a legitimate property, but to my knowledge (which isn't very much, considering that I just started looking at OTL today and haven't used it yet), you would have to write your own ASync
procedure in order to write to the property in any type of meaningful way.
Out of the box, the Param property is meant to be used with OTL's lower-level functionality. For example:
FHelloTask :=
CreateTask(RunHello, 'Hello')
.SetParameter('Delay', 1000)
.SetParameter('Message', 'Hello')
.OnMessage(HandleTaskMessage)
.OnTerminated(HandleTaskTerminated)
.Run;
That code will create a thread and run the 'RunHello' procedure, which can access Delay
and Message
via it's task parameter. I.e. msg := task.Param['Message'];
ASync is basically a wrapper around CreateTask
that automatically adds it into a thread pool, throws in some IOmniTaskConfig support, and has some error handling stuff. All in all, it's a class method with like 20 lines of code. It probably wouldn't be a terribly difficult matter to make your own version of ASync that implements the SetParameter procedure. I'm personally too tired to meddle with it right now, though, and I'd rather finish learning about OTL before I start hacking it up. Also, it's pretty easy to just use an anonymous method to capture the variables.