title: How do I properly use the WaitForSingleObject method to wait for an external program to terminate?
body: <p>I'm trying to launch an external application with elevated status, and wait until it exits before continuing:</p>
  
  <pre><code>var
    FProcess: THandle;
    ExecInfo: TShellExecuteInfo;
  begin
  
    FillChar(ExecInfo, SizeOf(ExecInfo), 0);
    with ExecInfo do
    begin
      cbSize := SizeOf(ExecInfo);
      fMask := 0;
      Wnd := AWindow;
      lpVerb := 'runas';
      lpFile := PChar(APath);
      lpParameters := PChar(AParams);
      lpDirectory := PChar(AWorkDir);
      nShow := SW_NORMAL;
    end;
  
    Result := ShellExecuteEx(@ExecInfo);
  
    if Wait then
    begin
      while WaitForSingleObject(ExecInfo.hProcess, INFINITE) &lt;&gt; WAIT_TIMEOUT do
        Application.ProcessMessages;
    end;
  </code></pre>
  
  <p>This launches, but it just keeps waiting.  The calling program never continues past the call to WaitForSingleObject, even after the called program exits.</p>
  
  <p>I've tried WAIT_OBJECT_0 instead of WAIT_TIMEOUT, but I have the same problem. What am I doing wrong here?</p>