Interrupt While/Loop?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
doctorafterman
Posts: 26
Joined: 26 Aug 2019, 08:45

Interrupt While/Loop?

29 Aug 2019, 10:11

Hi, I'm trying to use the same hotkey to break my loop/while.

Here's a simple example where I'm testing this by spamming myself with texts. I tried a loop toggle but that didn't work for me.

Whenever the hotkey is pressed again I need the script to stop where it is, and if it's pressed another time restart from the beginning

Code: Select all

#MaxThreadsPerHotkey 2
CoordMode, Mouse, Screen
Return
Esc::ExitApp

F7::
Day1Run := !Day1Run

If (!Day1Run)
Return

While (Day1Run)
{
Click 100, 240
Send (phone number)
Send {tab}{tab}
Send spamming you text{Enter}
sleep 1000
Click 100, 240
Send (phone number)
Send {tab}{tab}
Send Another one lol{Enter}
sleep 1000
}
return
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Interrupt While/Loop?

29 Aug 2019, 10:47

That code works well.
Pressing F7 again stops the while loop as expected.

What's the behavior you observe and how do you want it to be instead?
doctorafterman
Posts: 26
Joined: 26 Aug 2019, 08:45

Re: Interrupt While/Loop?

29 Aug 2019, 11:01

I need to interrupt the loop. I'm actually using this now and it works but I suppose I was wondering if there was a way to stop a loop at any point the HK is pressed rather than placing if-break statements everywhere?

Code: Select all

#MaxThreadsPerHotkey 2
CoordMode, Mouse, Screen
Return

f7::
Day1Run := !Day1Run

If (!Day1Run)
{
return
}

Loop
{
Click 100, 240
sleep 100
Send Phone Number
sleep 100
Send {tab}{tab}
sleep 7000
If (!Day1Run)
{
Break
}
Send spamming you text{Enter}
sleep 1000
}
return
Getfree wrote:
29 Aug 2019, 10:47
That code works well.
Pressing F7 again stops the while loop as expected.

What's the behavior you observe and how do you want it to be instead?
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Interrupt While/Loop?

29 Aug 2019, 12:02

Hallo,
Interrupts at any time within sleep:

Code: Select all

#MaxThreadsPerHotkey 2
CoordMode, Mouse, Screen
Return
Esc::ExitApp
F7::
If Day1Run := !Day1Run
Loop
{
	Click 100, 240
	Send (phone number)
	Send {tab}{tab}
	Send spamming you text{Enter}
	sleep(1000,Day1Run)
	Click 100, 240
	Send (phone number)
	Send {tab}{tab}
	Send Another one lol{Enter}
	sleep(1000,Day1Run)
}
return
Sleep(Time,ByRef On:=True)
{ ;like "Sleep, Time", but if On becomes Zero, the Thread ends
    End:= A_TickCount + Time
    While, S:= End-A_TickCount > 0
        IF On
            Sleep,S>100?100:S
        Else Exit
} ;a long Sleep will be segmented to be able to interrupt it fast
You can replace

Code: Select all

F7::
If Day1Run := !Day1Run
with

Code: Select all

F7::If !Day1Run := !Day1Run

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Raymondbit and 372 guests