Multiple While Loops?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BaconCatBug
Posts: 7
Joined: 15 Jun 2014, 10:08

Multiple While Loops?

15 Jun 2014, 10:27

Hi all. I currently have a script that I run in the background that waits for a program to launch, then runs a script when it detects the program has launched and closes it when it is closed:

Code: Select all

#NoTrayIcon 
#SingleInstance force
While, 1
{
WinWaitActive, WindowName
 run X:\Path\To\Compiled\Script.exe
 WinWaitClose,
 Process, Close, Script.exe
}
However, I'd like to add a second loop for a second, unrelated program. Simply adding a second while loop to the bottom doesn't seem to work, so I am unsure as to how to proceed.

Code: Select all

#NoTrayIcon 
#SingleInstance force
While, 1
{
WinWaitActive, WindowName
 run X:\Path\To\Compiled\Script.exe
 WinWaitClose,
 Process, Close, Script.exe
}

While, 2
{
WinWaitActive, WindowName2
 run X:\Path\To\Compiled\Script2.exe
 WinWaitClose,
 Process, Close, Script2.exe
}
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Multiple While Loops?

15 Jun 2014, 11:35

you could try using timers:

Code: Select all

#NoTrayIcon 
#SingleInstance force

SetTimer, WatchWin1, 1000
SetTimer, WatchWin2, 1000
return   ;// end of auto-execute section

WatchWin1:
   WinWaitActive, WindowName
   run X:\Path\To\Compiled\Script.exe
   WinWaitClose,
   Process, Close, Script.exe
return

WatchWin2:
   WinWaitActive, WindowName2
   run X:\Path\To\Compiled\Script2.exe
   WinWaitClose,
   Process, Close, Script2.exe
return
untested

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Multiple While Loops?

15 Jun 2014, 12:09

Wouldn't you need a negative period to run the timers once and keep the loops?
14.3 & 1.3.7
Gay-Lussac

Re: Multiple While Loops?

15 Jun 2014, 19:41

[quote="flyingDman"]Wouldn't you need a negative period to run the timers once and keep the loops?[/quote]
I am interested in this script (by OP). What would be your recommendation?
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Multiple While Loops?

15 Jun 2014, 19:57

flyingDman wrote:Wouldn't you need a negative period to run the timers once and keep the loops?
this seems retarded to me, but yea that might work if you want to keep the loops

of course the OP could also just create two separate scripts as well

BaconCatBug
Posts: 7
Joined: 15 Jun 2014, 10:08

Re: Multiple While Loops?

16 Jun 2014, 04:08

Gay-Lussac wrote:
flyingDman wrote:Wouldn't you need a negative period to run the timers once and keep the loops?
I am interested in this script (by OP). What would be your recommendation?
As am I. :)
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Multiple While Loops?

16 Jun 2014, 14:04

Code: Select all

; Reference: http://www.autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/

Gui, Helper:+LastFound
hWnd := WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall("RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage(MsgNum, "ShellMessage" )
return

ShellMessage(wParam, lParam) {
	static windows := {}
	;ToolTip, % "wParam: " wParam "`nlParam: " lParam "`n" (WinActive("A") = lParam)

	if (wParam = 1) {	; 1 = HSHELL_WINDOWCREATED
		if (a := WinActive("ahk_class MSPaintApp")) { ; Opens notepad when MSPaint is opened
			Run, Notepad.exe,,, WinPID
			windows.Insert([a, WinPID]) ; Stores the ahk_id of the MSPaint window and the AHK_pid of the Notepad window
		}
		if (a := WinActive("Disk Defragmenter")) { ; Opens solitaire when Disk Defragmenter is opened
			Run, C:\Program Files\Microsoft Games\sol.exe,,, WinPID
			windows.Insert([a, WinPID])
		}
	}
	
	else if (wParam = 2) {	; 2 = HSHELL_WINDOWDESTROYED
		for key, val in windows
			if (val.1 = lParam) {	; Closes the companion window if the window closed was one of the ones stored
				WinClose, % "ahk_pid " val.2
				windows.Remove(key)
				return
			}
	}
}

Esc::ExitApp
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Multiple While Loops?

16 Jun 2014, 18:42

Very clever! Works nicely (until you uncomment the tooltip line...). Do not know how much you leaned on SKAN's work, but it also shows the brilliant work he has contributed (and this back in 2007...).
14.3 & 1.3.7
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Multiple While Loops?

17 Jun 2014, 10:57

flyingDman wrote:Very clever! Works nicely (until you uncomment the tooltip line...). Do not know how much you leaned on SKAN's work, but it also shows the brilliant work he has contributed (and this back in 2007...).
Haha! :) Yes, all the credit goes to SKAN, I just followed his template. And yes, it works great. This is THE way I deal with waiting for windows, provided they can be "seen" by this technique.
The tooltip was just meant to be instructive.
BaconCatBug
Posts: 7
Joined: 15 Jun 2014, 10:08

Re: Multiple While Loops?

18 Jun 2014, 17:44

Excellent stuff Kon!

One problem I am having though, the kill part of the script only seems to work if the second program has a window associated with it. In my case, it's actually a compiled AHK script that runs in the background with no Icon or window.

I assumed I need to change:

Code: Select all

WinClose, % "ahk_pid " val.2
to

Code: Select all

Process,Close, % "ahk_pid " val.2
But as that doesn't work I also assume I am doing something wrong. Once again, thanks for your assistance!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Multiple While Loops?

19 Jun 2014, 00:24

You were on the right track. Comparing the docs for Process and WinClose we see:
Process, Cmd, PID-or-Name [, Param3]
...
PID-or-Name
This parameter can be either a number (the PID) or a process name as described below...

vs

WinClose [, WinTitle, WinText, SecondsToWait, ExcludeTitle, ExcludeText]
...
WinTitle
A window title or other criteria identifying the target window. See WinTitle.
PID-or-Name just requires the PID number, whereas WinTitle requires the text "ahk_pid " and the PID number.

Process, Close, % val.2
BaconCatBug
Posts: 7
Joined: 15 Jun 2014, 10:08

Re: Multiple While Loops?

19 Jun 2014, 05:42

Awesomesauce. Thank you again <3

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, JoeWinograd, mikeyww, Nerafius, Spawnova and 107 guests