-1

I would like to execute an application with parameters without a gui window, then auto end process gracefully in 2minutes.

This is how I use the code:

procedure ExecNoWait(Filename: string);
var
   bchExec: array[0..1024] of char;
   pchEXEC: Pchar;
   si: TStartupInfo;
   pi: TProcessInformation;
begin
   pchExec := @bchExec;
   StrPCopy(pchExec,Filename);
   FillChar(si,sizeof(si),0);
   FillChar(pi,sizeof(pi),0);
   si.dwFlags:=STARTF_USESHOWWINDOW;
   si.wShowWindow:=SW_HIDE;
   si.cb := sizeof(si);
   CreateProcess(Nil,pchExec,Nil,Nil,false,CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, Nil,Nil,si,pi);
end;


ExecNoWait(TheExeFilePath + '\ThisExe.exe -runnow'); //NON-GUI App

But I want to auto end or auto close the ThisExe.exe gracefully in 2minutes time.

How do we solved this?

thanks

XenKid
  • 161
  • 3
  • 16
  • 4
    There is no such thing as *graceful termination* of a process. Terminating a process is always rude, and with extreme prejudice. – IInspectable Jan 02 '14 at 03:29
  • 2
    Probably your best option would be to post `WM_CLOSE` or `WM_QUIT` to the process and hope it handles those messages properly. Check this: http://stackoverflow.com/questions/2055753/how-to-gracefully-terminate-a-process – rsrx Jan 02 '14 at 05:02
  • @MarkoPaunovic: The app being run has no GUI, though, so no window to post messages to. – Remy Lebeau Jan 02 '14 at 09:23
  • @RemyLebeau that depends. CMD.EXE reports to closing window and every console app should respond to Windows Shutdown/Reboot alert. So it boils down to the question if controlled application is buggy or correct – Arioch 'The Jan 02 '14 at 10:27
  • Do you have control over ThisExe application? Just add into it any means of IPC (interprocess communications) so it could receive shutdown request and close itself. – Arioch 'The Jan 02 '14 at 10:32
  • 1
    @XenKid Graceful termination involves co-operation. What mechanism does the other process offer for graceful termination? – David Heffernan Jan 02 '14 at 12:22

1 Answers1

3

CreateProcess() returns a THandle to the new process in the TProcessInformation record. You can pass that handle to WaitForSingleObject() with a 2-minute timeout. If the timeout elapses, you can then pass the same handle to TerminateProcess() (since there is no GUI involved, there is no option for a "graceful" termination), eg:

var
  bchExec: string;
  si: TStartupInfo;
  pi: TProcessInformation;
begin
  bchExec := Filename;
  UniqueString(bchExec);
  FillChar(si, sizeof(si), 0);
  FillChar(pi, sizeof(pi), 0);
  si.cb := sizeof(si);
  si.dwFlags := STARTF_USESHOWWINDOW;
  si.wShowWindow := SW_HIDE;
  if CreateProcess(nil, PChar(bchExec), nil, nil, false, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi) then
  begin
    CloseHandle(pi.hThread);
    if WaitForSingleObject(pi.hProcess, 120000) = WAIT_TIMEOUT then
      TerminateProcess(pi.hProcess, 666);
    CloseHandle(pi.hProcess);
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • So does CMD have GUI or not? It can be closed gracefully. And what about Windows Services that mostly are GUI - less? – Arioch 'The Jan 02 '14 at 10:30
  • @Arioch'The CMD has a close button, but using it is not equivalent to gracefully closing, the running program is aborted. Windows Services should handle the close command and can be stopped gracefully. – The_Fox Jan 02 '14 at 15:25
  • Windows services are controlled by the SCM. Use the SCM API, not `CreateProcess()`, to interact with services. Look at [`StartService()`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686321.aspx) and [`ControlService()`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682108.aspx) and other related functions. – Remy Lebeau Jan 02 '14 at 16:51
  • @The_Fox I am not asking about running programs, but about CMD itself. You can run CMD-script and when you would click that button, CMD would gracefully ask to cancel script execution. Also I think programs like FAR and nFTP and other console-targeted would also react to close and close in a proper manner. – Arioch 'The Jan 02 '14 at 18:22
  • @RemyLebeau what does it change in the global claim that any program w/o GUI cannot be closed gracefully ? It only is a matter of communication implemented at BOTH sides. GUI program can also be buggy and ignore close requests just like text-UI one. But that does not mean GUI programs cannot be closed gracefully in general, it only means one particular program is buggy. – Arioch 'The Jan 02 '14 at 18:24
  • GUI apps are designed to be closed gracefully - issue a `WM_QUIT` message to each window to break out of their message loops. Closing the console window forces all console processes to terminate, after a Ctrl+Close notification is first sent to them so they can clean themselves up. Ctrl+Close cannot be aborted, unlike Ctrl-C and Ctrl+Break, which can. – Remy Lebeau Jan 02 '14 at 18:39
  • Very good experts here, but I'm still confused, I'll get back for more testing. thanks – XenKid Jan 03 '14 at 10:53