Hey I'm trying to run an external program, with parameters but I don't actually want it to wait on execution. I've tried quite a few things and currently have :-
RunCommand(exeFile, cline, GOutP, [poNoConsole], swoShowNormal);
This just waits for execution is there a way to stop it doing that please?
I do such via threads. Using flags or status to tell the invoking procedure (or another) to determine if the command has completed.
The other thread can signal completion in various ways. I like to use the pointer passed to the thread to point at a boolean or state value (care to make sure the stack hasn't released, or messily with a global as below).
Here is a hastily written and tested program (that works on a mac in Terminal) where the command is running in a thread detached from the main thread:
(You'll need to complete the wrapper - I added this to an existing program - don't forget to add cthreads and classes to the uses clause).
Adjust the command for whatever OS you're on, (and the RunCommand details).
So for your purpose the "de-coupling" occurs on the BeginThread call. How you "rendez-vous" for the result is entirely up to your imagination.
You can also use a call to BeginThread, get the ThreadID and then have a different proc or function WaitForThreadTerminate (TID).
VAR
SexIsFunTerminated: boolean;
SexIsFunString: ansistring;
Function SexIsFun (p:pointer):ptrint;
VAR
Q : ansistring;
Result: ansistring;
BEGIN
Q := ansistring(p^); // get the command from the invoker
RunCommand ('/bin/sh',['-c',Q],Result);
ansistring(p^) := Result; //replace the command with the result (yes: clumsy - but you can figure out better ways
SexIsFunTerminated := true;
exit(0);
END;
Procedure IssueFreedCommand;
BEGIN
SexIsFunTerminated := false;
SexIsFunString := 'ls -al'; //the command to be passed with the RunCommand function (this is Unix / Linux compatible)*
BeginThread (@SexIsFun, @SexIsFunString);
END;
Procedure SexConnection;
VAR
T: ansistring;
BEGIN
// do something useful, then
While NOT SexIsFunTerminated do sleep (100);
Writeln (SexIsFunString);
END;
Procedure Sex;
BEGIN
IssueFreedCommand;
SexConnection;
END;
* I believe there is a Windows extension (?) of sorts that gives windows' terminal "Unix like" commands - but not sure