Struggling with infinite loop

Get help with using AutoHotkey and its commands and hotkeys
spytfire

Struggling with infinite loop

28 Jan 2018, 14:28

Hello everybody,
Before I start, I just have to say that I search that simple thing to do not create this topic unnecessarily.
Said that, let me explain my issue.
Years ago I was learning AHK function, but now I don't remember much things. I create a infinite loop pretty simple (use some hotkey to on and another to off loop). But, recently I'm having some real problems about the time of the ending of loop.
When I press the key to put it off, it is not breaking immediatly, that just turn off when the loop complete the actions.

Here's my poor script:

Code: Select all

2::
    LoopRunning  := 1
    while (LoopRunning) 
    {
    Send {F7}
    Sleep 1100
    Send {F8}
    Sleep 1100
    Send {F6}
    Sleep 1100

    }
Space::
    LoopRunning  := 0
    Return
So, I really need to change that, because when I press "Space", it doesn't stop immediatly and this is been very bad for me.
I search on forum and tried this ones:

Code: Select all

Loop
{
If stop = 1
Break
Else
{
ControlSend,, 2, ahk_id %program2%
Sleep 2520
}
}
return

F4::
If stop = 1
stop = 0
Else
stop = 1
return
And this one:

Code: Select all

Loop { 
 if GetKeyState("F4","P")
  Break
ControlSend,, 2, ahk_id %program2% 
Sleep 2520 
}
But I couldn't text it pretty much, because I have some lines (sends) and not just one, so I don't know how to properly put my function inside the { } or something like that.

It's pretty simple, but Idk how to to it x.x
Someone can please help me here? :dance:

Btw, I use this command to make me do another stuffs, so I can't use Esc as another hotkey

Code: Select all

Esc::
Suspend
Return
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Struggling with infinite loop

28 Jan 2018, 15:37

Your loop is likely not ending immediately because you have over 3 seconds of sleeps in it that it needs to complete every time. If you want it to end earlier, you can put more checks for LoopRunning into your While. I would probably try something like this:

Code: Select all

2::
    LoopRunning  := 1
    OuterLoop:
    while (LoopRunning) 
    {
      Send, {F7}
      GoSub, InnerLoop
      Send, {F8}
      GoSub, InnerLoop
      Send, {F6}
      GoSub, InnerLoop
    }

    InnerLoop:
      Loop, 11
      {
        Sleep 100
        If !LoopRunning
          Break OuterLoop
      }
    Return
Return

Space::LoopRunning  := 0
That will check and potentially end the hotkey every 100ms (rather than every 3300ms).

BTW, you could also just make the hotkey a toggle (so that pressing 2 again stops it):

Code: Select all

#MaxThreadsPerHotkey 2

2::
    LoopRunning := !LoopRunning
    OuterLoop:
    while (LoopRunning) 
    {
      Send, {F7}
      GoSub, InnerLoop
      Send, {F8}
      GoSub, InnerLoop
      Send, {F6}
      GoSub, InnerLoop
    }

    InnerLoop:
      Loop, 11
      {
        Sleep 100
        If !LoopRunning
          Break OuterLoop
      }
    Return
Return
Last edited by Osprey on 28 Jan 2018, 17:29, edited 1 time in total.
luciga
Posts: 30
Joined: 01 Dec 2016, 08:52

Re: Struggling with infinite loop

28 Jan 2018, 15:59

Hum, I don't know why, but when I put on and off, everything works, but If I try to do again (press 2 to active one more time the loop) it doesn't works and nothing happens.

Edited: Btw, I am the spytfire =)
luciga
Posts: 30
Joined: 01 Dec 2016, 08:52

Re: Struggling with infinite loop

28 Jan 2018, 16:15

divanebaba wrote:Hi.
As you use sleep ... your script freeze and you cannot act nearly immediately as you want.
In this thread there are timer-solutions for a similar problem.
Look and try to modify.
I am sorry, but how can I modify that? Is it pressing a and b to send the number 1 and 2? Like a double infinite loop in one and Esc cancel everything?
Also, where can I put all Send commands there?

Code: Select all

check:
if (a = 1)
send 1 
if (b = 1)
send 2
return

Code: Select all

if (a = 1)
    Send {F7}
    Sleep 1100
    Send {F8}
    Sleep 1100
    Send {F6}
    Sleep 1100
if (b = 1) 
    Send {F10}
    Sleep 1100
    Send {F11}
    Sleep 1100
    Send {F12}
    Sleep 1100
nadjibSoft
Posts: 9
Joined: 29 Dec 2017, 14:57
Facebook: 100011403841640

Re: Struggling with infinite loop

28 Jan 2018, 16:29

I've made a video about that in my channel in youtube: search in youtube for:

"Autohotkey stop and restart a loop(loop toggle)"
luciga
Posts: 30
Joined: 01 Dec 2016, 08:52

Re: Struggling with infinite loop

28 Jan 2018, 16:41

nadjibSoft wrote:I've made a video about that in my channel in youtube: search in youtube for:

"Autohotkey stop and restart a loop(loop toggle)"
nadjibSoft, thanks very much for your support man, but in my case, I can't do a global loop, because in the same script I have many loops and hotkeys, also it does not fix my problem about the "immediatly stop looping".
Now I understand that the function sleep freeze my script and that's why it doesn't stopping in the same time I press the button, so I really need an alternative for Sleep function.

Return to “Ask For Help”

Who is online

Users browsing this forum: BNOLI, boiler, Google [Bot], killall4u, Smile_, yogurtpixel and 146 guests