multithreading - Kill child process from main app -
i have delphi server run python scripts in background (they run similar "python sync.py **params**
").
this scripts can things connect external servers, open ssh connections , scary stuff can hang.
i need detect if hang, , kill it. also, if server closed, , want kill process (if not, delphi server hang, disappear desktop invisible in background. cause issues later if server executed again).
by first try not work. process killed if close app, server hang.
so question if exist more reliable/better way kill child process.
now, save handle , gettickcount , this: "handle=tickcount", running ttimer each 4 seconds , see if process timeout:
procedure tfrmmain.checkprocess(sender: tobject); var i: integer; fecha:tdatetime; start, stop, elapsed,handle : cardinal; begin stop := gettickcount; := (fprocesos.count - 1) downto 0 begin start := strtoint( fprocesos.valuefromindex[i] ); elapsed := stop - start; //milliseconds //esta muerto el proceso?? if ((elapsed>timeout_process) or (ftimer.enabled=false)) begin handle := strtoint( fprocesos.names[i] ); terminateprocess(handle,0); closehandle( handle ); fprocesos.delete( ); logmsg('a process timed out!',msgerror); end; end;//for end;
i didn't understand how checking if process not responsive. if child processes have gui, can send message 1 of windows belonging process using sendmessagetimeout api function, , check if message processed within specified time or not.
here sample code uses tprocessinfo listing child processes current thread, , terminating of them not responsive - please take note since couldn't understand method detecting unresponsive processes, isprocessunresponsive function not implemented in code, , left you:
function isprocessunresponsive(const processid: cardinal): boolean; begin //use own technique determining if process not responsive. end; procedure terminateunresponsiveprocesses; var process: tprocessitem; processinfo : tprocessinfo; begin processinfo := tprocessinfo.create(nil); try process in processinfo.runningprocesses begin if process.parentprocessid = getcurrentprocessid if isprocessunresponsive(process.processid) process.terminateprocess; end; processinfo.free; end; end;
Comments
Post a Comment