-2

I have 2 programs in Delphi - a service and some child processes that may run in any user session (these start when the service starts and should be closed when the service stops).

When the service stops I have to close the child applications safely, to make them catch formClose/FormDestroy events.

The service cannot use desktop communication, so it cannot send WMs like WM_Close, etc., to those processes.

Calling TerminateProcess does not make formClose/FormDestroy events occur in my child processes ...

So, what method of child process termination may be used here?

Currently, the only idea we have is to run taskkill.exe /im process.exe in each user session - it somehow makes killed process to run formClose/FormDestroy. How does it work? Just by sending WM_CLOSE?

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Alexander M.
  • 761
  • 1
  • 7
  • 14
  • So there is no kind of IPC between the single service and the desktop apps? If they were linked via a pipe/socket/mapping or whatever the service could talk directly to the client and instruct it to close itself during its stop event. – Alex K. Oct 14 '16 at 15:20
  • 1
    *Appears* to be so: http://serverfault.com/a/151788 – Sertac Akyuz Oct 14 '16 at 15:23
  • Implement IPC and ask the processes to close – David Heffernan Oct 14 '16 at 15:30
  • "there is no kind of IPC between" - yes, not messaging between them, we try to build it w\o it.. – Alexander M. Oct 14 '16 at 15:36
  • "Appears to be so: serverfault.com/a/151788 – Sertac Akyuz 15 mins ago" - do not see an appropriate method there.... wm_close and terminateprocess are not working for us. the only possible solution is to run taskkill.exe in each process (or my app that will send wm_close) ? – Alexander M. Oct 14 '16 at 15:40
  • @Alexander - I don't understand your comment. You asked a question *"how does it work? just wm_close?"*. The answer is "it appears to be so". – Sertac Akyuz Oct 14 '16 at 15:46
  • https://support.microsoft.com/en-us/kb/178893 – Free Consulting Oct 14 '16 at 15:46
  • You will not get an authoritative answer for that question. Run a console application that doesn't have the `{$APPTYPE CONSOLE}` directive and try to terminate it without '/f'. Do this for every OS version your app targets. As long as you get *"This process can only be terminated forcefully"* you can safely use taskkill. – Sertac Akyuz Oct 14 '16 at 15:50
  • 2
    Just use a manual-reset [event object](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682655.aspx) in the global namespace. When it's time to shut down, signal that event, and have clients shut down in response. – IInspectable Oct 14 '16 at 16:44
  • 3
    That *"we try to build it w\o [IPC]"* is somewhat alarming. You cannot omit IPC and hope for a *"safe"* way to terminate clients. You need to pick. – IInspectable Oct 14 '16 at 16:47

1 Answers1

0

The best solution is some simple IPC. In this case, all you really need is a global manual-reset event object, as IInspectable already suggested.

However, if you aren't allowed to do it the right way, you could instead launch another child process to send window messages to the application(s) you want to close.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158