title: Why do I get "The handle is invalid" when waiting for a thread I've told to terminate?
body: <p>How can I properly stop a thread when an application is closing?</p>
  
  <p>I do this:</p>
  
  <pre><code>procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  begin
  if not Thread1.Finished
  then
    begin
      Thread1.Terminate;
      Thread1.WaitFor();
    end;
  end;
  </code></pre>
  
  <p>But on Thread1.WaitFor I have an error: "Thread Error: The handle is invalid (6)." If I do WaitForSingleObject(Thread1.Handle,infinite) instead of WaitFor all is ok.</p>
  
  <p>Why if I use Thread.freeonterminate := false then WaitFor works good? Explain me please what I do wrong. 
  As I understand I need to use "if Assigned" instead of "if not Thread1.Finished", right?</p>