title: How do I reliably wait on a thread that has just been created?
body: <p>Consider the following program:</p>
  
  <pre><code>program TThreadBug;
  {$APPTYPE CONSOLE}
  
  uses
    SysUtils, Classes, Windows;
  
  type
    TMyThread = class(TThread)
    protected
      procedure Execute; override;
    end;
  
  procedure TMyThread.Execute;
  var
    i: Integer;
  begin
    for i := 1 to 5 do begin
      Writeln(i);
      Sleep(100);
    end;
  end;
  
  procedure UseTThread;
  var
    Thread: TMyThread;
  begin
    Writeln('TThread');
    Thread := TMyThread.Create;
    Thread.Terminate;
    Thread.WaitFor;
    Thread.Free;
    Writeln('Finished');
    Writeln;
  end;
  
  procedure UseTThreadWithSleep;
  var
    Thread: TMyThread;
  begin
    Writeln('TThreadWithSleep');
    Thread := TMyThread.Create;
    Sleep(100);
    Thread.Terminate;
    Thread.WaitFor;
    Thread.Free;
    Writeln('Finished');
    Writeln;
  end;
  
  begin
    UseTThread;
    UseTThreadWithSleep;
    Readln;
  end.
  </code></pre>
  
  <p>The output is:</p>
  
  <pre>
  TThread
  Finished
  
  TThreadWithSleep
  1
  2
  3
  4
  5
  Finished
  </pre>
  
  <p>So it seems that, for some reason, the main thread has to wait some arbitrary amount of time before terminating and waiting for the worker thread. Am I right in thinking that this is a bug in <code>TThread</code>? Is there any way I can work around this? I expect that if I get my thread to signal that it has started (using an event), then that would work around the issue. But that makes me feel dirty.</p>