RunWait for multiple programs

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
azhata
Posts: 3
Joined: 23 Jul 2018, 05:23

RunWait for multiple programs

23 Jul 2018, 07:08

Hello,

I want to Loop through a folder containing shortcuts (*.lnk files) and then run each program (shortcut).
After all the launched programs have been closed, the user running the script should be logged off.

I figured I'd use RunWait for this, but then each program will only run after the program from the previous loop iteration has exited (obviously).

Is there a way to have RunWait start multiple programs at once, or a more suitable alternative for what I'm trying to do?

Here's my code:

Code: Select all

Loop, C:\Path\to\Shortcuts\*.lnk {
	RunWait, "%A_LoopFileLongPath%"
}

; logoff
Run, shutdown.exe /l /f, , Hide

; hotkeys
^!f::Send Foo
^!b::Send Bar
Any help would be much appreciated!
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: RunWait for multiple programs

23 Jul 2018, 09:33

Hello azhata, welcome to the forum
Try to save the ID's and wait while there is one running, for example:

Code: Select all

pIDs := []
Loop % "C:\Temp\to\Shortcuts\*.lnk" {
	Run, % A_LoopFileLongPath , ,,ID
	pIDs.Push(ID)
}
While (pIDs.MaxIndex())
 For Index, pID in pIDs  {
     Process , Exist, % pID
 	   If (!ErrorLevel) {
 		     pIDs.RemoveAt(Index)
     }
 }
MsgBox 0x40000,, % "DONE!!"
; put your code here
Donec Perficiam
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: RunWait for multiple programs

24 Jul 2018, 06:49

@jmeneses , thank you for the script
azhata
Posts: 3
Joined: 23 Jul 2018, 05:23

Re: RunWait for multiple programs

24 Jul 2018, 07:11

Hello jmeneses, thanks for the welcome and for the script you provided!

Correct me if I'm wrong, but I think your solution will introduce a slight performance penalty (I'm seeing quite a bit of CPU utilization for the script process).
I suppose adding a Sleep inside the While loop could mitigate this, but that doesn't seem very clean.

Is there maybe a more efficient way of doing this?

Thanks!
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: RunWait for multiple programs

24 Jul 2018, 07:25

azhata wrote:Hello jmeneses, thanks for the welcome and for the script you provided!

Correct me if I'm wrong, but I think your solution will introduce a slight performance penalty (I'm seeing quite a bit of CPU utilization for the script process).
I suppose adding a Sleep inside the While loop could mitigate this, but that doesn't seem very clean.

Is there maybe a more efficient way of doing this?

Thanks!
Yes, yes,of course, you're right, better to put a SLEEP according to your CPU processor
Donec Perficiam
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: RunWait for multiple programs

24 Jul 2018, 07:29

azhata wrote: Is there maybe a more efficient way of doing this?
Thanks!
I can not think of another way, but surely if there is some way more elegant :thumbup:
Donec Perficiam
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: RunWait for multiple programs

27 Jul 2018, 06:02

azhata wrote:Is there maybe a more efficient way of doing this?
The code will be all the more efficient if the amount of time to sleep is appropriately chosen depending on your situation. The most efficient way is maybe to register a hook but would be at least overkill. Alternatively, you can use Process, WaitClose:

Code: Select all

a := []
Loop, parse, % "notepad,mspaint", CSV
{
	Run, % A_LoopField,,, PID
	a.push(PID)
}
while (PID:=a.pop())
    Process, WaitClose, % PID
; put your code here
Btw, use a timer to prevent runwait from postponing a loop iteration:

Code: Select all

#Persistent

Loop, parse, % "notepad,mspaint", CSV
{
	f := Func("RunWaitCloseAll").bind(A_LoopField)
	SetTimer % f, -1
}
return

RunWaitCloseAll(_target) {
static _n := 0
_n++
RunWait % _target
if not (--_n)
	MsgBox
}
my scripts
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RunWait for multiple programs

27 Jul 2018, 07:17

You could create a window group, and repeatedly use WinGet's Count subcommand.
GroupAdd - Syntax & Usage | AutoHotkey
https://autohotkey.com/docs/commands/GroupAdd.htm
WinGet - Syntax & Usage | AutoHotkey
https://autohotkey.com/docs/commands/WinGet.htm
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 407 guests