Is there a function to detect how long a loop has been running?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kusokamisama
Posts: 5
Joined: 14 Jan 2018, 07:08

Is there a function to detect how long a loop has been running?

21 Apr 2018, 21:01

Sometimes, an unexpected popup will cause a line in the script to keep looping until it is noticed and manually fixed to allow it to continue.

Is there a function to detect how long a loop has ran and notify if it has ran for over x minutes?

Thanks!
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Is there a function to detect how long a loop has been running?

21 Apr 2018, 21:27

Not unless you make one. But it sounds like your problem could be fixed if given a different approach. Instead of a loop, try using SetTimer and do your check as the first thing in the sub/label. Give it a second check to keep track of how many times that check was successful, then return. If the check fails and it continues, you can reset the counter. Here's an example:

Code: Select all

checkCnt:=0
setTimer,checkThing,100
return

checkThing:
if(!winActive("ExampleWindow")){
    checkCnt++
    if(checkCnt > 100){ ; if the if-block has be ran 100 times (100ms between each time, so 10000ms/10s)
        ; do something to fix it, like close the popup
    }
    return
}
checkCnt:=0
; do normal thing
return
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Is there a function to detect how long a loop has been running?

22 Apr 2018, 04:05

Code: Select all

x := (A_ScreenWidth // 2) - 30
y := (A_ScreenHeight // 2) + 80
initialTimestamp := A_TickCount

Loop, 25 {
	ToolTip, % "Looping: " . A_Index, % x, % y
	Sleep, 50
}

MsgBox, % howLongHasItBeen(initialTimestamp)

howLongHasItBeen(initialTimestamp) {
	return (A_TickCount - initialTimestamp)
}

Code: Select all

x := (A_ScreenWidth // 2) - 30
y := (A_ScreenHeight // 2) + 80
DllCall("QueryPerformanceFrequency", "Int64*", frequency)
DllCall("QueryPerformanceCounter", "Int64*", initialTimestamp)

Loop, 25 {
	ToolTip, % "Looping: " . A_Index, % x, % y
	Sleep, 50
}

MsgBox, % howLongHasItBeen(initialTimestamp, frequency)

howLongHasItBeen(initialTimestamp, frequency) {
	DllCall("QueryPerformanceCounter", "Int64*", currentTimestamp)
	duration := (currentTimestamp - initialTimestamp) * 1000 ; ms
	duration /= frequency
	return duration
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 207 guests