I have a Delphi server that run python scripts in the background (they run similar to "python Sync.py **params**
").
This scripts can do things like connect to external servers, open ssh connections and scary stuff where they can hang.
I need to detect if they hang, and kill it. Also, if the server is closed, and want to kill the process too (if not, the Delphi server hang, disappear from the desktop but get invisible in the background. This cause issues later if the server is executed again).
By first try not work. The process get killed but if I close the app, the server hang.
So the question is if exist a more reliable/better way to kill the child process.
Now, I save their Handle and their GetTickCount , like this: "handle=tickcount", then running with a TTimer each 4 seconds and see if the process timeout:
procedure TfrmMain.CheckProcess(Sender: TObject);
var
i: Integer;
Fecha:TDateTime;
start, stop, elapsed,Handle : cardinal;
begin
stop := GetTickCount;
for i := (FProcesos.Count - 1) downto 0 do
begin
start := StrToInt( FProcesos.ValueFromIndex[i] );
elapsed := stop - start; //milliseconds
//Esta muerto el proceso??
if ((elapsed>TIMEOUT_PROCESS) or (FTimer.Enabled=False)) then
begin
Handle := StrToInt( FProcesos.Names[i] );
TerminateProcess(Handle,0);
CloseHandle( Handle );
FProcesos.Delete( i );
LogMsg('A process timed out!',msgError);
end;
end;//for
end;