title: Waiting for multiples threads using WaitForMultipleObjects
body: <p>I'm using the <code>WaitForMultipleObjects</code> function to wait for the finalization of several threads, but I'm doing something wrong because the result is not the expected </p>
  
  <p>see this sample code </p>
  
  <pre><code>type
    TForm1 = class(TForm)
      Memo1: TMemo;
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    private
    end;
  
    TFoo = class(TThread)
    private
      Factor: Double;
      procedure ShowData;
    protected
      procedure Execute; override;
      constructor Create(AFactor : Double);
    end;
  
  
  var
    Form1: TForm1;
  
  implementation
  
  Uses
   Math;
  
  {$R *.dfm}
  
  { TFoo }
  
  constructor TFoo.Create(AFactor: Double);
  begin
    inherited Create(False);
    Factor := AFactor;
    FreeOnTerminate := True;
  
  end;
  
  procedure TFoo.Execute;
  const
    Max=100000000;
  var
    i : Integer;
  begin
    inherited;
    for i:=1 to Max do
      Factor:=Sqrt(Factor);
  
    Synchronize(ShowData);
  end;
  
  procedure TFoo.ShowData;
  begin
    Form1.Memo1.Lines.Add(FloatToStr(Factor));
  end;
  
  procedure TForm1.Button1Click(Sender: TObject);
  const
   nThreads=5;
  Var
   tArr  : Array[1..nThreads]  of TFoo;
   hArr  : Array[1..nThreads]  of THandle;
   i     : Integer;
   rWait : Cardinal;
  begin
    for i:=1  to nThreads do
     begin
       tArr[i]:=TFoo.Create(Pi*i);
       hArr[i]:=tArr[i].Handle;
     end;
  
    repeat
      rWait:= WaitForMultipleObjects(nThreads, @hArr, True, 100);
      Application.ProcessMessages;
    until rWait&lt;&gt;WAIT_TIMEOUT;
    //here I want to show this message when all the threads are terminated    
    Memo1.Lines.Add('Wait done');
  end;
  
  end.
  </code></pre>
  
  <p>this is the current output of the demo app</p>
  
  <pre><code>1
  Wait done
  1
  1
  1
  1
  </code></pre>
  
  <p>but I want something like this</p>
  
  <pre><code>1
  1
  1
  1
  1
  Wait done
  </code></pre>
  
  <p>How I must use the  <code>WaitForMultipleObjects</code> function to wait until all the thread are terminated?</p>