little help stuck in loop Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Expose96
Posts: 30
Joined: 02 Mar 2017, 13:50

little help stuck in loop

Post by Expose96 » 16 Jan 2022, 10:10

Hey ho! Long time i posted here.
Ive used some scripts from different people and tried to combine them but cant get it to work somehow.
I have a small GUI that indicates "ON" or "OFF" it looks really cool im happy with how it looks. but when i put my loop in the "ON" section it stays on "ON". It is stuck in its loop Any help?

Code: Select all

SoundPlay, audio\start.mp3
sleep 3000
#MaxThreadsPerHotkey 2
#warn
leftbound:= A_ScreenWidth/2-5
rightbound:= A_ScreenWidth/2+5
topbound:= A_ScreenHeight/2-5
bottombound:= A_ScreenHeight/2+5
Modified=20130110
Filename1=gui
setworkingdir,%a_scriptdir%
Gui,2:Font,Cdefault,Fixedsys
Gui,2:Color,Black
z:=""
Gui,2:Color, EEAA99
Gui,2:Add,Progress, x10 y20 w100 h23 Disabled BackgroundGreen vC2
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB2 gStart,ON
Gui,2:Add,Progress, xp yp wp hp Disabled BackgroundRED vC1
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB1 gStart,OFF
Gui,2: Show, x10 y1 w200 h60 ,%filename1%
Gui 2:+LastFound +ToolWindow +AlwaysOnTop 
WinSet, TransColor, EEAA99
Gui,2:-Caption
return
~$F1::
start:
gui,2:submit,nohide
z:=!z
if z
{
GuiControl,2: hide,B1
GuiControl,2: hide,C1
GuiControl,2: show,B2
GuiControl,2: show,C2

loop
{
CoordMode, Pixel, Screen
PixelSearch, FoundX, FoundY, leftbound, topbound, rightbound, bottombound, 0xA145A3, 35, Fast RGB
If (ErrorLevel = 0){
Sleep 10
Click
}
}

}
else
{
GuiControl,2: hide,B2
GuiControl,2: hide,C2
GuiControl,2: show,B1
GuiControl,2: show,C1
}
return
F4::
ExitApp
Thanks!

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: little help stuck in loop

Post by mikeyww » 16 Jan 2022, 10:24

Without any code or condition indicating when to break out of the loop, your loop is infinite, because that is what a loop does.

garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: little help stuck in loop  Topic is solved

Post by garry » 16 Jan 2022, 10:35

Code: Select all

try
SoundPlay, audio\start.mp3
sleep 3000
#MaxThreadsPerHotkey 2
#warn
CoordMode, Pixel, Screen
leftbound  := A_ScreenWidth/2-5
rightbound := A_ScreenWidth/2+5
topbound   := A_ScreenHeight/2-5
bottombound:= A_ScreenHeight/2+5

Modified=20130110
Filename1=gui
setworkingdir,%a_scriptdir%
Gui,2:Font,Cdefault,Fixedsys
Gui,2:Color,Black
z:=""
Gui,2:Color, EEAA99
Gui,2:Add,Progress, x10 y20 w100 h23 Disabled BackgroundGreen vC2
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB2 gStart,ON
Gui,2:Add,Progress, xp yp wp hp Disabled BackgroundRED vC1
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB1 gStart,OFF
Gui,2: Show, x10 y1 w200 h60 ,%filename1%
Gui 2:+LastFound +ToolWindow +AlwaysOnTop 
WinSet, TransColor, EEAA99
Gui,2:-Caption
return
;-------------------------
2Guiescape:
2Guiclose:
Exitapp
esc::exitapp
~$F4::
exitapp
;-------------------------
~$F1::
start:
gui,2:submit,nohide
z:=!z
if z
{
GuiControl,2: hide,B1
GuiControl,2: hide,C1
GuiControl,2: show,B2
GuiControl,2: show,C2
  loop
    {
    if (!z)
      break
    tooltip,running
    PixelSearch, FoundX, FoundY, leftbound, topbound, rightbound, bottombound, 0xA145A3, 35, Fast RGB
    If (ErrorLevel = 0)
      {
      Sleep 10
      Click
      }
    }
}
else
{
GuiControl,2: hide,B2
GuiControl,2: hide,C2
GuiControl,2: show,B1
GuiControl,2: show,C1
tooltip,OFF
sleep,2000
tooltip
}
return
;=============================================================

Expose96
Posts: 30
Joined: 02 Mar 2017, 13:50

Re: little help stuck in loop

Post by Expose96 » 16 Jan 2022, 10:46

Hey thanks for the replys!
unfortunatly even when the GUI is "off" the loop is still active
thanks for the fast reply!

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: little help stuck in loop

Post by mikeyww » 16 Jan 2022, 10:54

z starts on and so the loop never ends. You can add whatever condition you want to end it. In some cases like this, using a timer instead of a loop is a way to address the issue.
Last edited by mikeyww on 16 Jan 2022, 10:55, edited 1 time in total.

Expose96
Posts: 30
Joined: 02 Mar 2017, 13:50

Re: little help stuck in loop

Post by Expose96 » 16 Jan 2022, 10:54

IT WORKS!!!!
i just had like 3 other open files thats why it was working in "off"
Thank you!!!

garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: little help stuck in loop

Post by garry » 16 Jan 2022, 12:48

@mikeyww thank you, I see the problem if ONE loop takes a long time to run . Can you give another example ?
( if the loop is for shorttime then it's OK to use F1 otherwise break with F4 ( or maybe close also program process ) )

Code: Select all

/*
start script , Button is OFF
F1 start     , Button is ON 
   loop is running until use F1 again > Button is OFF
   but you can't break if something running , ( here keeps msgbox for 10 seconds )
   you can break msgbox running when use F4  ( RELOAD )
If the loop is for shorttime then it's OK to use F1
ESC to quit this script
*/

;- a test script for loop

#MaxThreadsPerHotkey 2
#warn
Filename1=Colorbotton
setworkingdir,%a_scriptdir%
Gui,2:Font,Cdefault,Fixedsys
Gui,2:Color,Black
z:=""
Gui,2:Color, EEAA99
Gui,2:Add,Progress, x10 y20 w100 h23 Disabled BackgroundGreen vC2
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB2 gStart,ON
Gui,2:Add,Progress, xp yp wp hp Disabled BackgroundRED vC1
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB1 gStart,OFF
Gui,2: Show, x10 y1 w200 h60 ,%filename1%
Gui 2:+LastFound +ToolWindow +AlwaysOnTop 
WinSet, TransColor, EEAA99
Gui,2:-Caption
return
;-------------------------
F4::reload
esc::exitapp
;-------------------------
~$F1::
start:
gui,2:submit,nohide
z:=!z
if z
{
GuiControl,2: hide,B1
GuiControl,2: hide,C1
GuiControl,2: show,B2
GuiControl,2: show,C2
  loop
    {
    if (!z)
       break
    msgbox, 262208,PIXELSEARCH ,Running,.5
    sleep,1000
    msgbox, 262208,PIXELSEARCH-CONTINUE ,Wait 10 seconds`nOR use F4 to Reload,10
    }
;Until (!z)
msgbox, 262208,PIXELSEARCH,LOOP ENDED in ON-Sector,3
}
else
{
GuiControl,2: hide,B2
GuiControl,2: hide,C2
GuiControl,2: show,B1
GuiControl,2: show,C1
msgbox, 262208,,You are in OFF-Sector,1
}
return
;=============================================================
a short script , toggle with F9 to immediately break a loop

Code: Select all

;- a test script for loop / see Button ON/OFF 
;- if ONE loop takes longtime break with F9 and start again with F9

#Warn
#MaxThreadsPerHotkey 2
Filename1=Colorbotton
setworkingdir,%a_scriptdir%
Gui,2: -dpiscale -caption +Alwaysontop
Gui,2:Font,CYellow S14,Lucida Console
z:=""
Gui,2:Add,Progress,x0 y0 w300 h60 Disabled BackgroundGreen vC2
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB2 gStart,Loop-ON (F9)
Gui,2:Add,Progress, xp yp wp hp Disabled BackgroundRED vC1
Gui,2:Add,Text, xp yp wp hp cYellow BackgroundTrans Center 0x200 vB1 gStart,Loop-OFF (F9)
Gui,2: Show,x100 y100 w300 h60 ,%filename1%
return
;-------------------------
esc::exitapp
;-------------------------
$F9::
start:
gui,2:submit,nohide
z:=!z
if (z)
{
GuiControl,2: hide,B1
GuiControl,2: hide,C1
GuiControl,2: show,B2
GuiControl,2: show,C2
  loop
    {
    if (!z)
       break
    sleep,200
    msgbox, 262208,LOOP-Running Continous ,This waits 10 seconds`nOR use F9 again to Reload`nESC to quit this script,10
    }
;Until (z)     ;- loop only once
;msgbox, 262208,LOOP-END,LOOP ENDED in ON-Sector,2
return
}
else
  reload
return
;=======================================================
Last edited by garry on 16 Jan 2022, 14:29, edited 1 time in total.

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: little help stuck in loop

Post by mikeyww » 16 Jan 2022, 13:08

You are right that when z becomes false, the loop will break. Thanks.

Post Reply

Return to “Ask for Help (v1)”