I try to accomplish 10 (for example) tasks at a time, but according to the result of the execution of the code below, I receive, instead of the 1st second, 2-3 seconds. What am I doing wrong and how to fix it, maybe there is another way?
procedure TForm1.Button3Click(Sender: TObject); var tasks: array of ITask; value: integer; i: integer; Start, Stop: integer; Elapsed: integer; begin Start := GetTickCount; // засекли начало выполнения операции Setlength(tasks, 10); value := 0; TParallel.For(0, 10 - 1, procedure(i: integer) begin tasks[i] := TTask.Create( procedure() begin Sleep(1000); TInterlocked.Add(value, 1000); end); tasks[i].Start; end); TTask.WaitForAll(tasks); Stop := GetTickCount; // засекли окончание выполнения операции ShowMessage(FloatToStr((Stop - Start) / 1000) + #10 + 'Всего: ' + value.ToString); end;