cztomczak / phpdesktop

Develop desktop GUI applications using PHP, HTML5, JavaScript and SQLite
https://groups.google.com/d/forum/phpdesktop
2.69k stars 572 forks source link

A way to launch external processes/servers during startup or later, and automatic shutdown of these processes when app exits #163

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

User can execute applications using php's system() command or similar, but there is no easy way to end long running processes when phpdesktop application closes.

Option 1 - "execute_on_startup" in settings.json

User wants to run node.js server along with phpdesktop. That server should start when phpdesktop starts and shut down automatically when phpdesktop app exits.

Solution: In settings.json add option "execute_on_startup" which would be a list of .exe, allowed would be relative paths (phpdesktop dir) or full paths.

There is also an idea for "execute_on_shutdown" option, see Issue #186.

Option 2 - new JS API: phpdesktop.LaunchProcess()

While app runs user wants to run an external process. Currently this can be done using XMLHttpRequest to a php script that calls php's system() command or similar. Unfortunately there is no easy way to end this process when phpdesktop app exits. For example this process runs a long task, but user exits application early, so this process should be killed automatically.

Solution: Add new function to JavascriptApi: LaunchProcess(). It should have some options whether the app window should be hidden. Use the ShellExecuteEx function and save process handle (SHELLEXECUTEINFO.hProcess) for later to end the process when app exits.

ShellExecuteEx: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx

SHELLEXECUTEINFO: https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784(v=vs.85).aspx

Original issue reported on code.google.com by czarek.t...@gmail.com on 7 Mar 2015 at 8:34

GoogleCodeExporter commented 9 years ago

It is possible to close external processes currently, but only with some additional .bat script and WMI queries. This has been explained here: https://groups.google.com/d/msg/phpdesktop/PLC0dMz0Pp8/6kCC_VNTP_wJ

Such .bat script would check whether phpdesktop.exe process still runs and when it's not running anymore it could end external processes launched by php's system() command. It's a bit complicated, but feasible. Your app's processes names shold be unique to make it reliable.

Original comment by czarek.t...@gmail.com on 7 Mar 2015 at 8:43

cztomczak commented 7 years ago

Related: Issue #186 ("An option in settings.json to specify a path to a script or exe that will be executed when application shuts down").

bkris commented 6 years ago

Somebody knows something about this feature? Is this planned to be implemented or already does?

cztomczak commented 6 years ago

There is no known schedule for implementing this feature.

cztomczak commented 6 years ago

This functionality can be accomplished using NirCmd command-line utility, see:

  1. NirCmd utility and its various exec commands: https://github.com/cztomczak/phpdesktop/wiki/Knowledge-Base#how-do-i-call-external-programs-or-commands-asynchronously-in-background-without-waiting-for-them-to-end

  2. Example code for shutdown using "waitprocess" command in NirCmd: https://github.com/cztomczak/phpdesktop/issues/186#issuecomment-376860052

jal617b commented 4 years ago

Another option is to use a script (Autohotkey or AutoIt etc) to create an app launcher.

ApplicationFolder |AppLauncher.ahk // this can be compiled to .exe | phpdesktopfolder |__phpdesktop-chrome.exe

Contents of AppLauncher.ahk

Run, notepad.exe // whatever app you want to run
Run, phpdesktopfolder\phpdesktop-chrome.exe
WinSetTitle, PhpDesktop App //main_window title of your phpdesktop app based on settings.json
WinWait, PhpDesktop App  //wait until your app is closed
WinWaitClose  
Process, Close, notepad.exe
MsgBox, Notepad is now closed.
avega93 commented 4 years ago

A solution that worked for me is to set the port on "listen_on" option dynamically, in the first page of the app run a ".bat" file that close all process that I created before, in my case python.exe process. After this, create the process that you need. But this only works to prevent crash on re-open. Not kill all the process after close. Sorry for my bad english.

darkterminal commented 2 years ago

I have pull example how to create Windows Print Service on start up and on shutdown application just feel free to take a look https://github.com/cztomczak/phpdesktop/pull/305 maybe it's help you to know how it's work for me. I forgot to write settings.json value listen_on with fixed value in README.md example.