title: If I call ShellExecuteEx, why does GetExitCodeProcess say the program terminated even though it's still running?
body: <p>I want to open a file which is initially saved to SQL table, but is saved to disk before the call to ShellExecuteEx. Once it is saved I now have a valid file path and theoretically should be able to use this function to accomplish my goal. I need the program to open the file in its appropriate program and wait until that program closes before continuing. Currently the code will launch the correct application and open the passed file, but it does this without waiting (which I know because I display a message to indicate when the application terminates) and the app I wrote which is supposed to launch the correct program closes. It displays the message, then launches the program. I will admit that I do not fully understand how ShellExecuteEx works and have used code I found on the web in conjunction with my code to achieve the desired result. Below you will find the code. Any help would be greatly appreciated.</p>
  
  <pre><code>procedure Fire(FileStr: String);
  var
    SEInfo: TShellExecuteInfo;
    ExitCode: DWORD;
    ExecuteFile, ParamString, StartInString: string;
  begin
    ExecuteFile:= FileStr;
    FillChar(SEInfo, SizeOf(SEInfo), 0) ;
    SEInfo.cbSize := SizeOf(TShellExecuteInfo);
    with SEInfo do 
    begin
      fMask := SEE_MASK_NOCLOSEPROCESS;
      Wnd := Application.Handle;
      lpFile := PChar(ExecuteFile) ;
      nShow := SW_SHOWNORMAL;
    end;
  
    if ShellExecuteEx(@SEInfo) then
    begin
      repeat
        Application.ProcessMessages;
        GetExitCodeProcess(SEInfo.hProcess, ExitCode) ;
      until (ExitCode &lt;&gt; STILL_ACTIVE) or Application.Terminated;
      ShowMessage('App terminated') ;
    end
      else ShowMessage('Error starting App') ;
  end;
  </code></pre>
  
  <p>I realized that the the file I was writing to disk hadn't finished which is why the message appeared before the program. Adding another application.Processmessages after the call to write the file to disk resolved this. GetExitCodeProcess still does not return the value of STILL_ACTIVE while the called app is open, though.</p>
  
  <p>After doing more research I have decided to open a new question that is more pointed at what I am trying to accomplish. If anyone is interested in following it, you can find it here </p>
  
  <p><a href="https://stackoverflow.com/questions/19734258/what-is-the-best-way-to-execute-an-external-app-via-delphi">How do I wait to delete a file until after the program I started has finished using it?</a></p>