title: How to synchronize between displaying the processor and running the loops in Delphi Parallel.For?
body: <p>I have a <code>Parallel.For()</code> loop in my program as follows:</p>
  <pre class="lang-pascal prettyprint-override"><code>procedure TForm1.Button1Click(Sender: TObject);
  var
    n,m:integer;
    handle: THandle;
  begin
    //BeforeTest;
    ProgressBar1.Position:=0;
    handle := Form1.Handle;
    n:=10;
    m:=100;
    SetLength(list,n*m);
  
    FTask := TTask.create(
      procedure
      var
        processed: integer; // shared counter
        total    : integer; // total number of items to be processed
      begin
        processed := 0;
        total:=n*m;
        TParallel.For(1,total,
          procedure(Index: Integer)
          var
            new: integer;
          begin
            new := TInterlocked.Increment(processed); // increment the shared counter
            if (new mod 10) = 0 then // update the progress bar every 10 processed items
              PostMessage(handle, WM_UPDATE_PROGRESS, Round(new / total * 100), 0);
            //Calculation procedure
            Calc(Index,Total);
          end);
      
        // Update the UI
        TThread.Queue(nil, AfterTest);
      end); // TTask.Run
    FTask.Start;
  </code></pre>
  <p>and in the <code>AfterTest()</code> procedure:</p>
  <pre class="lang-pascal prettyprint-override"><code>procedure TForm1.AfterTest;
  begin
    FTask:=nil;
    ShowMessage('Finished');
  end;
  </code></pre>
  <p>When I run the program, the time to complete the ProgressBar and display the &quot;Finished&quot; message is different. The ProgressBar fills up sooner. How do I synchronize the time between &quot;Finished&quot; message and the ProgressBar?</p>