title: Delphi TTask WaitForAll vs. Synchronise
body: <p>I have only limted experience with threads. I want to read some fundamental database tables in parallel and I need to wait until all tables are read before the program can reasonably progress. In that regard, blocking the main thread is OK for me.</p>
  
  <p>This code (simplified) works fine:</p>
  
  <pre><code>procedure ReadDBMultiThread;
    var ATasks : Array of ITask;
  begin
    SetLength(ATasks, 3);
    ATasks[0] := TTaskCreate(procedure() begin DB_ReadTable1; end);
    ATasks[1] := TTaskCreate(procedure() begin DB_ReadTable2; end);
    ATasks[2] := TTaskCreate(procedure() begin DB_ReadTable3; end);
  
    ATasks[0].Start;
    ATasks[1].Start;
    ATasks[2].Start; 
  
    TTask.WaitForAll(ATasks);
  end;
  </code></pre>
  
  <p>However, let's say I would like to update the main form to show the progress, i.e. which database table has already been read (or do any other necessary main thread work). Obviously, I cannot use Synchronise() because that would lead to a dead lock with WaitForall() and I cannot use Queue() because that would by executed after WaitForAll() finishes.</p>
  
  <p>So, is there a good solution to solve this "WaitForAll vs Synchronise" situation? I guess, this must be a situation many people come into... the need to wait for all being finished, but wanting to update the main thread...</p>
  
  <p>I thought about something like this, given in pseudo-code, replacing the WaitForAll() statement:</p>
  
  <pre><code>repeat 
    Applicaton.ProcessMessages; // or "ProcessSynchroniseMessages"
  until "AllTaskCompleted"(ATasks);
  </code></pre>
  
  <p>Would this work? Is there a better solution?</p>
  
  <p>Could I write a own routine like ProcessMessages, but restricted to Synchronise messages, i.e. other main form events not being executed until later?</p>
  
  <p>Many thanks in advance!</p>