How to break out of loop immediately upon GUI button press? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Cuadrix
Posts: 199
Joined: 07 May 2017, 08:26

How to break out of loop immediately upon GUI button press?

14 Mar 2018, 15:02

Just like the subject title says. I have a simple GUI with 2 buttons, Start and Stop. When I press the Start button, the loop will begin. When i press the Stop Button, it should immediately break out of the loop, and not wait till the current iteration is over or wait till it meets the Break command.

Code: Select all

Gui, Add, Button, x50 y50 w50 h20 gStart, Start
Gui, Add, Button, x+15 y50 w50 h20 gSTop, Stop
Gui, Show,w200 h100, Gui
Return

Start:
Loop, 6
{
	MsgBox, One
	Sleep, 1500
	MsgBox, Two
	Sleep, 1500
	MsgBox, Three
	Sleep, 1500
	MsgBox, Four
	Sleep, 1500
	MsgBox, Five
	Sleep, 1500
	MsgBox, Six
	Sleep, 1500
}
Return

Stop:
	;Something to stop the loop in Start label here?
Return
If there is anyway to forcibly break out of the loop, I would be delighted to hear about it.
----
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: How to break out of loop immediately upon GUI button press?

14 Mar 2018, 15:47

If there is nothing else in the code, perhaps Reload would do the trick?
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
Cuadrix
Posts: 199
Joined: 07 May 2017, 08:26

Re: How to break out of loop immediately upon GUI button press?  Topic is solved

14 Mar 2018, 16:06

Not really. The goal is to make the script not reload, if possible.
The closest I have come to what I am asking for is this, but it doesn't look very nice, and it takes up more time with those added If and else checks:

Code: Select all

Gui, Add, Button, x50 y50 w50 h20 gStart, Start
Gui, Add, Button, x+15 y50 w50 h20 gSTop, Stop
Gui, Show,w200 h100, Gui
Return

Start:
var = 1
If(var = 1)
{
Loop, 6
	{
		If(var = 1)
		MsgBox, One
		Else Break
		If(var = 1)
		Sleep, 1500
		Else Break
		If(var = 1)
		MsgBox, Two
		Else Break
		If(var = 1)
		Sleep, 1500
		Else Break
		If(var = 1)
		MsgBox, Three
		Else Break
		If(var = 1)
		Sleep, 1500
		Else Break
		If(var = 1)
		MsgBox, Four
		Else Break
		If(var = 1)
		Sleep, 1500
		Else Break
		If(var = 1)
		MsgBox, Five
		Else Break
		If(var = 1)
		Sleep, 1500
		Else Break
		If(var = 1)
		MsgBox, Six
		Else Break
		If(var = 1)
		Sleep, 1500
		Else Break
	}
}
Return

Stop:
var = 0
Return
----
guest3456
Posts: 2547
Joined: 09 Oct 2013, 10:31

Re: How to break out of loop immediately upon GUI button press?

15 Mar 2018, 06:30

i can't think of any other way than by checking for the variable as you've demonstrated. its not elegant at all. but this is obviously not a real world usecase, so perhaps you can restructure your real code differently to achieve the same outcome


Return to “Ask For Help”

Who is online

Users browsing this forum: Chunjee, Delta Pythagorean, Google [Bot], Kobaltauge, rediffusion, snowmind, teadrinker, vsub and 258 guests