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.
Starting and closing process Topic is solved
Starting and closing process
Last edited by ellie59 on 08 Mar 2017, 08:41, edited 3 times in total.
Re: Starting and ending two processes at the same time
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
Re: Starting and ending two processes at the same time
Any solution, including the one above and this one, might fail if the launched process starts another process and exits.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.
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
}
}
Re: Starting and ending two processes at the same time
Thank you so much for your help
We do our own scripts to learn how to work with AutoHotkey
We're using "Process, WaitClose," which I think is the correct solution.
Can you advise me what should the correct syntax of the script?
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
Can you advise me what should the correct syntax of the script?
Re: Starting and ending two processes at the same time
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.
Re: Starting and ending two processes at the same time
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
Re: Starting and ending two processes at the same time
Process WaitClose is thread blocking, meaning that nothing below that command is executed while that command is waiting.ellie59 wrote:I am using PID but do not know how to set WaitClose that the condition is sufficient for one off process.
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 )
Re: Starting and ending two processes at the same time
Ok,
I want to learn ahk (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?
I want to learn ahk (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?
Re: Starting and ending two processes at the same time
Or does anyone know another solution to avoid the use (I try to not use a timer or a loop)
Re: Starting and ending two processes at the same time
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.
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.
Re: Starting and ending two processes at the same time
How to fix and optimize the script for multiple processes, still confused please help
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
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: Starting and closing process
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.
Not using any loops or timers seems like a pretty arbitrary restriction.
FG
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
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
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
Re: Starting and closing process
Pretty much the same solution I posted like 2 weeks ago, but he/she didn't like it.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.
Re: Starting and closing process
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
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
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
Re: Starting and closing process
FanaticGuru:
I can not use absolute paths because the files are portable USB
Can you please edit the script (for these processes)
(1X.exe - 5X.exe > REPLACE > "FirefoxPortable.exe" and "proxyCV.exe") ↓↓↓↓↓↓↓↓↓↓
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
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: Starting and closing process
The script now assumes a path and does not use %A_WorkingDir% as the path.ellie59 wrote:FanaticGuru:
I can not use absolute paths because the files are portable USBCode: Select all
run, F:\mozilla firefox portable\FirefoxPortable.exe,,PID run, C:\proxy\proxy soft\proxyCV.exe,,PID -f ./proxy/proxy soft/SettingMAIN
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
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
}
;}
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
)
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
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
Re: Starting and closing process
Oh, thank you !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
Re: Starting and closing process
FanaticGuru:
Maybe I expressed badly , 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)
PS: DriveLetter It's a very interesting solution. I'm sure he will use in the future, other script thank you.
Maybe I expressed badly , 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.
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: Starting and closing process
I am not sure what current folder means.ellie59 wrote:FanaticGuru:
Maybe I expressed badly , 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 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
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
Who is online
Users browsing this forum: Anput, jameswrightesq and 284 guests