I want to start a potentially long running background process from Delphi. I want to leave the process running independently, but first I want to check that the process started OK.
If anything went wrong on startup, I want to capture any output written to standardErr and log it. If the background process starts OK, my program needs to be able to exit and leave the spawned process running.
The psuedo code would be something like this:
process:=RunProgramInBackground('someCommand.exe');
sleep(1000); // Wait a bit to see if the program started OK
if process.Finished and process.ExitCode=FAIL then
Raise Exception.Create(process.ErrorStream);
process.Dispose; // Close any connection we may still have to the running process
Program.Exit; // Background process keeps running
I’ve looked at a few things (WinExec, CreateProcess, ShellExecute, JclMiscel) but can’t find any examples for what I’m trying to do. What is the best way to do this?
I’m using Delphi 2010
The background process is a 3rd party program I don’t have the source to.
I ended up using the example No’Am linked to, and adding code to check that the process started OK. This function only checks the exit code of the background process, it doesn’t read the StdErr output.
Here is what I did: