It should sound natural at this point that task definition passes through anonymous methods. The following snippet shows how easy it is to define a new task through the PPL:
uses System.Threading;
TTask.Run(
procedure
begin
// Do something
end
);
We should recall the TThread.CreateAnonymousThread class function but a bit more abstracted. The anonymous method will be executed as soon as this task gets assigned to a thread of the PPL pool.
There is no magic—the underlying elements are all still there (threads, synchronization needs, and so on). In other words, this is the typical case of syntax sugar added over a classic problem.
Our example can be translated using PPL elements as shown in the PPLProject demo (full source code included). The following excerpt is from the OnClick event handler of StartButton:
procedure TMainForm.StartButtonClick(Sender: TObject);
begin
if Assigned(FTask) then
Exit;
FTask := TTask.Run(
procedure
var...