Break a loop inside a loop with a key

Get help with using AutoHotkey and its commands and hotkeys
Guest

Break a loop inside a loop with a key

14 Jan 2018, 22:22

Code: Select all

Loop {
 sleep 5000
      Wait2()
   }

      Wait2()
{
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
	else sleep 10000 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		break
	}
}
This loop searches a color. If it doesn't find it, it goes to sleep.
How can I break this sleep immediately with a key?
mmmzq
Posts: 8
Joined: 10 Jan 2018, 00:31

Re: Break a loop inside a loop with a key

14 Jan 2018, 23:04

Either Check GetKeyState or add a determine statement like

Code: Select all

breakloop := false
Loop {

    if breakloop
        break
        
 sleep 5000
      Wait2()
   }

      Wait2()
{
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
	else sleep 10000 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		break
	}
}
return

esc::
breakloop := true
return
Guest

Re: Break a loop inside a loop with a key

14 Jan 2018, 23:24

mmmzq wrote:Either Check GetKeyState or add a determine statement like

Code: Select all

breakloop := false
Loop {

    if breakloop
        break
        
 sleep 5000
      Wait2()
   }

      Wait2()
{
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
	else sleep 10000 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		break
	}
}
return

esc::
breakloop := true
return
Thank you. I don't want to break the first loop, I want to break the second loop. Can I break this 10 second sleep somehow?
mmmzq
Posts: 8
Joined: 10 Jan 2018, 00:31

Re: Break a loop inside a loop with a key

15 Jan 2018, 00:10

游客 wrote:
mmmzq wrote:Either Check GetKeyState or add a determine statement like

Code: Select all

breakloop := false
Loop {

    if breakloop
        break
        
 sleep 5000
      Wait2()
   }

      Wait2()
{
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
	else sleep 10000 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		break
	}
}
return

esc::
breakloop := true
return
Thank you. I don't want to break the first loop, I want to break the second loop. Can I break this 10 second sleep somehow?
Try set up a timer to substitute the sleep line

Code: Select all

Sleeptime := 0
Settimer, Sleep, 1000
while sleeptime <= 10 and not breakloop
	continue    
Then somewhere out the loop:

Code: Select all

Sleep:
	sleeptime += 1
return
lexikos
Posts: 6968
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Break a loop inside a loop with a key

15 Jan 2018, 03:03

while sleeptime <= 10 and not breakloop
continue
I would suggest not using a loop which just "continues". It will loop as fast as possible, needlessly consuming CPU time.

If you are using a loop, there is no need for a timer.

Code: Select all

while A_Index <= 100 and not breakloop
    Sleep 100
or

Code: Select all

loop 100
    Sleep 100
until breakloop
You can still set breakloop from a hotkey or any other event, and you can adjust the numbers to check more or less frequently if needed.

There may be some inaccuracy caused by "drift", as each iteration may take longer than 100 ms. If you want more accuracy, you can check the time:

Code: Select all

start := A_TickCount
while (A_TickCount - start) < 10000 and not breakloop
    Sleep 100
This may still go as much as 100 ms over 10 seconds.

If you use Sleep 10000, there is no way to cause it to return early. You can only interrupt it and do something else in the meantime (such as execute another hotkey or timer).
Guest

Re: Break a loop inside a loop with a key

16 Jan 2018, 04:17

lexikos wrote:
Sorry, I don't know where to put these lines.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], diemerson, JoeWinograd, RandomPersonOnAHK, TAC109 and 44 guests