Starting and closing process Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Starting and closing process

23 Feb 2017, 18:03

Hello all,

Looking commands (setup.ahk) to run two processes (1.exe and 2.exe), When I turn off one process (example: 2.exe),(setup.ahk) It found that one process is ends, and ends the second process (example: 1.exe),(setup.ahk) then turns off.

excuse my bad English.
Last edited by ellie59 on 08 Mar 2017, 08:41, edited 3 times in total.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Starting and ending two processes at the same time

23 Feb 2017, 18:35

I think you might want something like this code. Be sure to update what 1 and 2.exes are, and their directory to run them.

Code: Select all

#Persistent
#SingleInstance
Run, C:\Users\ellie59\1.exe
Run, C:\Users\ellie59\2.exe
SetTimer, check, 500
return

check:
Process, Exist, 1.exe
If (ErrorLevel=0) ; process not found
Process, Close, 2.exe
Process, Exist, 2.exe
If (ErrorLevel=0)
Process, Close, 1.exe
return
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Starting and ending two processes at the same time

23 Feb 2017, 19:11

ellie59 wrote:Hello all,
Looking commands (setup.ahk) to run two processes (1.exe and 2.exe), When I turn off one process (example: 2.exe),(setup.ahk) It found that one process is ends, and ends the second process (example: 1.exe),(setup.ahk) then turns off.
excuse my bad English.
Any solution, including the one above and this one, might fail if the launched process starts another process and exits.
This can handle more than 2 process ... ( press F1 2-3 times )

Code: Select all

#singleinstance force

Global myProcesses := ["mspaint.exe", "notepad.exe"]
Global myProcessesIds := []

F10::
ExitApp

F9::Reload

F1::
	startAll()
Return

startAll() {
	for i, proc in myProcesses {
		Run % proc,,, processID
		myProcessesIds.Push(processID)
	}
	fn := Func("Monitor")
	SetTimer % fn, -1
}

monitor() {
	static processClosed := False
	while(!processClosed) {
		for i, processID in myProcessesIds {
			Process, Exist, % processID
			if(!ErrorLevel) {
				processClosed := True
				Break
			}
		}
		sleep 1
	}
	closeAll()
	processClosed := False
}

closeAll() {
	loop % myProcessesIds.Length()
	{
		processID := myProcessesIds.Pop()
		Process Close, % processID
	}
}
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and ending two processes at the same time

23 Feb 2017, 20:58

Thank you so much for your help
We do our own scripts to learn how to work with AutoHotkey

Code: Select all

#Persistent
#SingleInstance

Run, %A_WorkingDir%\1.exe
Run, %A_WorkingDir%\2.exe

Process, WaitClose, 1.exe or 2.exe
If (ErrorLevel=0)
Process, Close, 1.exe and 2.exe
ExitApp
return
We're using "Process, WaitClose," which I think is the correct solution.
Can you advise me what should the correct syntax of the script?
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Starting and ending two processes at the same time

23 Feb 2017, 22:01

ellie59 wrote:TWe're using "Process, WaitClose," which I think is the correct solution.
Can you advise me what should the correct syntax of the script?
  • ProcessID is more reliable than ProcessName since you can run multiple processes with the same name.
  • I don't think WaitClose can handle multiple process names.
    https://autohotkey.com/docs/commands/Process.htm wrote:... for ALL matching processes to close.
  • When the docs says 'ALL processes' I think it means all instances with the same name.
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and ending two processes at the same time

23 Feb 2017, 22:24

Code: Select all

#Persistent
#SingleInstance

Run, %A_WorkingDir%\1.exe,,,PID1
Run, %A_WorkingDir%\2.exe,,,PID2

Process, WaitClose, %PID1%
Process, WaitClose, %PID2%
If (ErrorLevel=0)
Process, Close, %PID1%
Process, Close, %PID2%
ExitApp
return
I am using PID but do not know how to set WaitClose that the condition is sufficient for one off process. :crazy:
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Starting and ending two processes at the same time

24 Feb 2017, 01:18

ellie59 wrote:I am using PID but do not know how to set WaitClose that the condition is sufficient for one off process. :crazy:
Process WaitClose is thread blocking, meaning that nothing below that command is executed while that command is waiting.
It is fine when monitoring a single process.
This is why both, I and Exaskryz, are using Process Exist at a set interval. ( both in a timer thread )
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and ending two processes at the same time

24 Feb 2017, 14:18

Ok,

I want to learn ahk :-D (I try to not use a timer or a loop)

#Persistent
#SingleInstance

Run, %A_WorkingDir%\1X.exe,,,PID1
Run, %A_WorkingDir%\2X.exe,,,PID2

If (ABC = "PID1" || "PID2") [If there is one rule not exist PID1 OR PID2 = ABC] But this rule does not work it is only as an example
{
Process, Waitclose, %ABC%
}

If (ErrorLevel=0)
Process, Close, %PID1%
Process, Close, %PID2%
ExitApp
return



You do not know the "IF" rule that would work for me?
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and ending two processes at the same time

24 Feb 2017, 15:58

Or does anyone know another solution to avoid the use (I try to not use a timer or a loop)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Starting and ending two processes at the same time

24 Feb 2017, 18:12

Using a timer or a loop is probably necessary. If you try to use WaitClose, you are only checking *one* process. So what if the other process closes first? AHK will not respond.

If you do know that you always want process 1 to close first, then using Process, WaitClose, 1.exe or Process, WaitClose, %PID1% could work. But doing so means if Program 2 closes first, then AHK do nothing and you'll still manually need to close Program 1 before AHK's script resumes.

It is like traffic lights. If you have two traffic lights on the road you're on, and they're both red, if the second one turns green, you can't go. You have to wait for the first one to turn green. Turning green is like closing the process in the AHK code when you use WaitClose -- you could imagine it as WaitGreen.
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and ending two processes at the same time

08 Mar 2017, 07:39

How to fix and optimize the script for multiple processes, still confused please help :cry:

Code: Select all

#Persistent
#SingleInstance

run, %A_WorkingDir%\1X.exe,,,PID1
run, %A_WorkingDir%\2X.exe,,,PID2
run, %A_WorkingDir%\3X.exe,,,PID3
run, %A_WorkingDir%\4X.exe,,,PID4
run, %A_WorkingDir%\5X.exe,,,PID5

settimer, X1, 500
return

X1:
Process, Exist, %PID1%
If (ErrorLevel=0)
Process, Close, %PID2%

Process, Exist, %PID1%
If (ErrorLevel=0)
Process, Close, %PID3%

Process, Exist, %PID1%
If (ErrorLevel=0)
Process, Close, %PID4%

Process, Exist, %PID1%
If (ErrorLevel=0)
Process, Close, %PID5%

Process, Exist, %PID2%
If (ErrorLevel=0)
Process, Close, %PID1%

Process, Exist, %PID3%
If (ErrorLevel=0)
Process, Close, %PID1%

Process, Exist, %PID4%
If (ErrorLevel=0)
Process, Close, %PID1%

Process, Exist, %PID5%
If (ErrorLevel=0)
Process, Close, %PID1%

; exitapp = (if not there %PID1%, %PID2%, %PID3%, %PID4%, %PID5%, > exit script)

return
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Starting and closing process

08 Mar 2017, 15:13

This script will start a list of programs, retain their PIDs, check all those PIDs every .5 seconds and close all the processes if any of the processes no longer exist.

Code: Select all

#Persistent
#SingleInstance

Exes =
(
1X.exe
2X.exe
3X.exe
4X.exe
5X.exe
)

PIDs := {}
Loop, Parse, Exes, `n
{
	run, %A_WorkingDir%\%A_LoopField%,,,PID
	PIDs.Push(PID)
}

SetTimer, CheckPIDs, 500

CheckPIDs:
	for index, PID in PIDS
	{
		Process, Exist, %PID%
		if !ErrorLevel
		{
			for index, PID in PIDS
				Process, Close, %PID%
			ExitApp
		}
	}
return
Not using any loops or timers seems like a pretty arbitrary restriction.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Starting and closing process

08 Mar 2017, 15:24

FanaticGuru wrote:This script will start a list of programs, retain their PIDs, check all those PIDs every .5 seconds and close all the processes if any of the processes no longer exist.
Pretty much the same solution I posted like 2 weeks ago, but he/she didn't like it.
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and closing process

08 Mar 2017, 18:41

4GForce: Your solution works and is good (but I'm new in AutoHotkey and your script is too complicated edit for me) Take it as a compliment :salute:

FanaticGuru: Thank you very much (I can understand your script and for me is instructive as an example) this is exactly what I needed.

Exaskryz: Also thank you
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and closing process

10 Mar 2017, 14:28

FanaticGuru:
I can not use absolute paths because the files are portable USB

Code: Select all

run, F:\mozilla firefox portable\FirefoxPortable.exe,,PID
run, C:\proxy\proxy soft\proxyCV.exe,,PID -f ./proxy/proxy soft/SettingMAIN

Can you please edit the script (for these processes)
(1X.exe - 5X.exe > REPLACE > "FirefoxPortable.exe" and "proxyCV.exe")

Code: Select all

run, %A_WorkingDir%\mozilla firefox portable\FirefoxPortable.exe,,PID
run, %A_WorkingDir%\proxy\proxy soft\proxyCV.exe,,PID -f ./proxy/proxy soft/SettingMAIN
↓↓↓↓↓↓↓↓↓↓

Code: Select all

#Persistent
#SingleInstance

Exes =
(
1X.exe
2X.exe
3X.exe
4X.exe
5X.exe
)

PIDs := {}
Loop, Parse, Exes, `n
{
	run, %A_WorkingDir%\%A_LoopField%,,,PID
	PIDs.Push(PID)
}

SetTimer, CheckPIDs, 500

CheckPIDs:
	for index, PID in PIDS
	{
		Process, Exist, %PID%
		if !ErrorLevel
		{
			for index, PID in PIDS
				Process, Close, %PID%
			ExitApp
		}
	}
return
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Starting and closing process

10 Mar 2017, 16:17

ellie59 wrote:FanaticGuru:
I can not use absolute paths because the files are portable USB

Code: Select all

run, F:\mozilla firefox portable\FirefoxPortable.exe,,PID
run, C:\proxy\proxy soft\proxyCV.exe,,PID -f ./proxy/proxy soft/SettingMAIN
The script now assumes a path and does not use %A_WorkingDir% as the path.

Code: Select all

#Persistent
#SingleInstance

; Path and Files (No path will probably default to folder of script)
Exes =
(
F:\mozilla firefox portable\FirefoxPortable.exe
C:\proxy\proxy soft\proxyCV.exe -f ./proxy/proxy soft/SettingMAIN
3X.exe
4X.exe
5X.exe
)

PIDs := {}
Loop, Parse, Exes, `n
{
	run, %A_LoopField%,,,PID
	PIDs.Push(PID)
}

SetTimer, CheckPIDs, 500

CheckPIDs:
	for index, PID in PIDS
	{
		Process, Exist, %PID%
		if !ErrorLevel
		{
			for index, PID in PIDS
				Process, Close, %PID%
			ExitApp
		}
	}
return
Every file has a path regardless of whether it is on a portable USB or not. On a server, on a network, on a CD, every file has a unique path.

If the drive letter changes everytime you plug in the USB drive that is a different issue. Maybe this will help you find the DriveLetter based on the drive label.

Code: Select all

;{ DriveLetter
; Fanatic Guru
; 2013 11 07
;
; FUNCTION that will find the drive letter based on drive label.
;
;---------------------------------------------------------------------------------
;
; Method:
;   DriveLetter(DriveLabel, Option*)
;
; Parameters:
;   1)  {DriveLabel} drive label to search for
;       Optional, Default = "" which returns all drive letters of proper type
;   2)  {Option} Variadic List of Options
;       {Option = "Exact"} require exact match including case sensitive 
;       {Option = "CDROM" or "REMOVABLE" or "FIXED" or "NETWORK" or "RAMDISK" or "UNKNOWN"} search for this type drive, if no type is given all types are searched
;       Optional, Default = Partial Matching, All Types
;
; Returns:
;   String containing the drive letters that match drive label
;
; Examples:      
; MsgBox % DriveLetter("Flash", "Exact", "Removable", "Ramdisk")        ; All removable and ramdisk drives named exactly "Flash"
; MsgBox % DriveLetter(, "Removable", "Ramdisk")                        ; All removable and ramdisk drives
; MsgBox % DriveLetter("Flash")                                         ; All drives named "Flash"
; MsgBox % DriveLetter()                                                ; All drives
;
DriveLetter(DriveLabel := "", Option*)
{
   Types := "CDROM|REMOVABLE|FIXED|NETWORK|RAMDISK|UNKNOWN"
   Loop, % Option.MaxIndex()
   {
      Option := Option[A_Index ]
      Loop, Parse, Types, |
      {
         If (Option = A_LoopField)
            Option_Type .= A_LoopField "|"
      }
      if (Option = "Exact")
         Option_Exact := true
   }
   if !Option_Type
      Option_Type := Types
   DriveGet DriveLetterList, List
   Loop, Parse, DriveLetterList
   {
      DriveGet Found_DriveLabel, Label, %A_LoopField%:
      DriveGet Found_DriveType, Type, %A_LoopField%:
      if InStr(Option_Type,Found_DriveType)
      {
         If Option_Exact
            {
               If (Found_DriveLabel == DriveLabel or !DriveLabel)
               {
                  DriveLetter .= A_LoopField
               }
            }
            else
            {
               If (InStr(Found_DriveLabel,DriveLabel) or !DriveLabel)
               {
                  DriveLetter .= A_LoopField
               }
            }
         }
      }
   return DriveLetter
}
;}
It might allow you to do something like:

Code: Select all

Exes =
(
% DriveLetter("LabelNameofUSBdrive") ":\mozilla firefox portable\FirefoxPortable.exe"
C:\proxy\proxy soft\proxyCV.exe -f ./proxy/proxy soft/SettingMAIN
3X.exe
4X.exe
5X.exe
)
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Starting and closing process

10 Mar 2017, 16:40

ellie59 wrote:4GForce: Your solution works and is good (but I'm new in AutoHotkey and your script is too complicated edit for me) Take it as a compliment :salute:
Oh, thank you !
User avatar
ellie59
Posts: 36
Joined: 23 Feb 2017, 17:41

Re: Starting and closing process

10 Mar 2017, 17:22

FanaticGuru:
Maybe I expressed badly :angel: , ABC folder will be moved frequently (To other folders and even USB) So the script must be portable and work with the current folder.

ABC\script.exe
ABC\mozilla firefox portable\FirefoxPortable.exe
ABC\proxy\proxy soft\proxyCV.exe


This solution does not work. (But that's what I try)

Code: Select all

#Persistent
#SingleInstance

Exes =
(
run, \mozilla firefox portable\FirefoxPortable.exe,,PID
run, \proxy\proxy soft\proxyCV.exe,,PID -f ./proxy/proxy soft/SettingMAIN
)

PIDs := {}
Loop, Parse, Exes, `n
{
	run, %A_WorkingDir%\%A_LoopField%,,,PID
	PIDs.Push(PID)
}

SetTimer, CheckPIDs, 500

CheckPIDs:
	for index, PID in PIDS
	{
		Process, Exist, %PID%
		if !ErrorLevel
		{
			for index, PID in PIDS
				Process, Close, %PID%
			ExitApp
		}
	}
return

PS: DriveLetter It's a very interesting solution. I'm sure he will use in the future, other script thank you.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Starting and closing process

10 Mar 2017, 18:17

ellie59 wrote:FanaticGuru:
Maybe I expressed badly :angel: , ABC folder will be moved frequently (To other folders and even USB) So the script must be portable and work with the current folder.
I am not sure what current folder means.

I imagine you would want A_WorkingDir or A_ScriptDir. If the programs you are wanting to run are in the same folder as the script then use A_ScriptDir.

Here are your choices:
A_WorkingDir
The script's current working directory, which is where files will be accessed by default. The final backslash is not included unless it is the root directory. Two examples: C:\ and C:\My Documents. Use SetWorkingDir to change the working directory.

A_ScriptDir
The full path of the directory where the current script is located. The final backslash is omitted (even for root directories).

A_WinDir
The Windows directory. For example: C:\Windows

A_ProgramFiles
The Program Files directory (e.g. C:\Program Files or C:\Program Files (x86)). This is usually the same as the ProgramFiles environment variable.

A_Desktop
The full path and name of the folder containing the current user's desktop files.

A_DesktopCommon
The full path and name of the folder containing the all-users desktop files.

A_StartMenu
The full path and name of the current user's Start Menu folder.

A_StartMenuCommon
The full path and name of the all-users Start Menu folder.

A_Programs
The full path and name of the Programs folder in the current user's Start Menu.

A_ProgramsCommon
The full path and name of the Programs folder in the all-users Start Menu.

A_Startup
The full path and name of the Startup folder in the current user's Start Menu.

A_StartupCommon
The full path and name of the Startup folder in the all-users Start Menu.

A_MyDocuments
The full path and name of the current user's "My Documents" folder. Unlike most of the similar variables, if the folder is the root of a drive, the final backslash is not included. For example, it would contain M: rather than M:\

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, jameswrightesq and 284 guests