Problems managing a long infinite loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Zafferg
Posts: 3
Joined: 16 Aug 2017, 04:36

Problems managing a long infinite loop

16 Aug 2017, 04:52

Good day all, huge fan of this program and have been an avid lurker for a year or so.

I have a script that automates a tedious part of my job which involves a lot of repeated mouse clicks with decent sleep sections so that I can see if anything has changed in different windows.

1 - It currently is working spot on apart from if I send the loop stop hotkey it will continue the loop until the end before stopping - which as you can see from all the sleeps can take a wee while - I'm guessing this is because the loop only checks for the stop flag once per cycle - is there a way to get it to constantly check for this flag and exit if found to be active?
2 - I've been playing with the pause toggle command and it does work to pause the script but my tooltips are not elegant and depending at the point in the loop it pauses the tooltip can be immediately overwritten - I'd really like a tooltip showing a 'Pause On' and 'Pause off' as well as the existing tooltips within the loop indicating location within the loop / program- is this possible?

Much love

Code: Select all

^g::
  $stop := 0
  Loop, 
  { 
    Tooltip, Script is still running G18 to quit G16 to pause- Currently Level 1
    Click 114, 685 
    Sleep, 3000
    Click 186, 82  
    sleep, 3000
    Click 1176, 161  
    sleep, 3000
    Click 1176, 161  
    sleep, 60000
    Tooltip, Script is still running G18 to quit G16 to pause- Currently Level 2
    Click 131, 701 ; Click on TS l2
    Click 186, 82  
    sleep, 3000
    Click 1176, 161  
    sleep, 3000
    Click 1176, 161  
    sleep, 60000
    if ($stop)
    {
      return
    }
  }

^f:: 
  $stop := 1
  Tooltip,
Return


^d::
	Pause
	Suspend
	Tooltip, Pause button pressed
return
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Problems managing a long infinite loop

16 Aug 2017, 05:52

Here's one way to do it, I've added a listbox element for you too which can be removed if not needed.

Code: Select all

Gui,+toolwindow +alwaysontop
Gui,Add,ListBox,x0 y0 w250 h70 vlbp
Gui,Show,x5 y5 w250 h69,Automation Window
return

guiclose:
exitapp

^f::stop := 1

^g::
stop := 0
Loop {
	DoClick(114,685,3000,"Clicking first item")
	DoClick(186,82,3000,"Clicking second item")
	DoClick(1176,161,3000,"Clicking third item")
	DoClick(1176,161,60000,"Waiting 1 minute, level 2")
	DoClick(131,701,100,"Clicking TS 12")
	DoClick(186,82,3000)
	DoClick(1176,161,3000)
	DoClick(1176,161,60000,"Waiting 1 minute, level 3?")
	if (stop)
		break
}
AddListItem("__________Script Idle__________")
msgbox Loop broken, script idle
return

AddListItem(item) {
	global lbp
	static array := [], maxItems := 5 ;set static array and itemcount
	array.insert(item)
	if (array.length() > maxItems) { ;if array holds more items than we want to show, remove the oldest
		array.remove(1)
	}
	tempList := ""
	guicontrol,,lbp,| ;clear listbox
	loop % array.length() { ;loop through array adding items to listbox
		tempList .= (a_index > 1 ? "|" : "") array[a_index]
	}
	guicontrol,,lbp,% tempList ;update listbox with items
}

DoClick(x,y,delay=100,item="") {
	global stop
	if (stop) ;if stop, return
		return
	if (item != "")
		AddListItem(item)
	Click,%x%,%y%
	endTime := A_Tickcount + delay
	while(a_tickcount < endTime and !stop) { ;small sleeps so loop can be broken instantly
		sleep 100
	}
}
User avatar
Zafferg
Posts: 3
Joined: 16 Aug 2017, 04:36

Re: Problems managing a long infinite loop

16 Aug 2017, 06:41

Dude you are an absolute legend thank you for the extensive advice and code, LOVE the automation window functionality takes all the guesswork out of it. Small issue that where it is currently placed is over one of my required mouse clicks, i tried shifting it down thus:

Code: Select all

Gui,+toolwindow +alwaysontop
Gui,Add,ListBox,x0 y196 w250 h70 vlbp
Gui,Show,x5 y201 w250 h69,Automation Window
return
But this seems to break the functionality of the listbox, it appears but no longer echos the current commands being sent - am I missing something?

This is the full code as it stands now with some minor editing of your original awesome work:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Gui,+toolwindow +alwaysontop
Gui,Add,ListBox,x0 y196 w250 h70 vlbp
Gui,Show,x5 y201 w250 h69,Automation Window
return

guiclose:
exitapp

^f::stop := 1

^g::
stop := 0
Loop {
	DoClick(114,685,3000,"Clicking TS L1")
	DoClick(186,82,3000,"Clicking KS Refresh Button")
	DoClick(1176,161,3000,"Clicking Object Sort")
	DoClick(1176,161,60000,"Clicking Object sort, Waiting 1 minute at Level 1")
	DoClick(131,701,100,"Clicking TS L2")
	DoClick(186,82,3000,"Clicking KS Refresh Button")
	DoClick(1176,161,3000,"Clicking Object Sort")
	DoClick(1176,161,60000,"Clicking Object sort, Waiting 1 minute at Level 2")
	if (stop)
		break
}
AddListItem("__________Script Idle__________")
msgbox Loop broken, script idle
return

AddListItem(item) {
	global lbp
	static array := [], maxItems := 5 ;set static array and itemcount
	array.insert(item)
	if (array.length() > maxItems) { ;if array holds more items than we want to show, remove the oldest
		array.remove(1)
	}
	tempList := ""
	guicontrol,,lbp,| ;clear listbox
	loop % array.length() { ;loop through array adding items to listbox
		tempList .= (a_index > 1 ? "|" : "") array[a_index]
	}
	guicontrol,,lbp,% tempList ;update listbox with items
}

DoClick(x,y,delay=100,item="") {
	global stop
	if (stop) ;if stop, return
		return
	if (item != "")
		AddListItem(item)
	Click,%x%,%y%
	endTime := A_Tickcount + delay
	while(a_tickcount < endTime and !stop) { ;small sleeps so loop can be broken instantly
		sleep 100
	}
}

User avatar
Zafferg
Posts: 3
Joined: 16 Aug 2017, 04:36

Re: Problems managing a long infinite loop

16 Aug 2017, 07:10

Never mind I was changing the incorrect parameter obviously

Code: Select all

Gui,+toolwindow +alwaysontop
Gui,Add,ListBox,x0 y0 w250 h70 vlbp
Gui,Show,x5 y300 w250 h69,Automation Window
return
Works perfectly

Again huge thanks for this help

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 312 guests