Hotkey to kill a looping thread in the same script

Get help with using AutoHotkey and its commands and hotkeys
WeThotUWasAToad
Posts: 260
Joined: 19 Nov 2013, 08:44

Hotkey to kill a looping thread in the same script

26 Nov 2018, 21:59

Hello,

Is there a command which will kill a looping thread contained in the same script without actually killing/closing the script?

For example, in the script shown below:

F1 triggers a looping script,
F2 successfully pauses F1 while it (F2) executes,
F3 is unassigned, and
F4 closes the script

Code: Select all

F1::
Loop, 60
{
var:=A_Index
SendRaw This is a loop at
Send {Space}
FormatTime, date,, yyyy-MMM-dd(ddd) h:mm:ss
Send %Date%
Send {Enter}
	Sleep, 1000
}
Return
;__________________________________________________________
F2::
SendRaw This is a 5 second interruption.
	Sleep, 5000
Send {Enter}
Return
;__________________________________________________________
;F3::
;__________________________________________________________
F4::ExitApp
Is there a command that can be attached to F3 which will not just pause but actually stop F1 without closing the script?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
swagfag
Posts: 2908
Joined: 11 Jan 2017, 17:59

Re: Hotkey to kill a looping thread in the same script

26 Nov 2018, 23:12

Code: Select all

#NoEnv
#SingleInstance Force
SetBatchLines -1
SetKeyDelay -1, -1

global breakLoop := false

F1::
breakLoop := false
Loop, 60
{
	var:=A_Index

	if breakLoop
		break

	breakSendRaw("This is a loop at")

	if breakLoop
		break

	Send {Space}

	if breakLoop
		break

	FormatTime, date,, yyyy-MMM-dd(ddd) h:mm:ss

	if breakLoop
		break

	breakSendRaw(date)

	if breakLoop
		break

	Send {Enter}

	if breakLoop
		break

	breakSleep(1000)

	if breakLoop
		break

}
breakLoop := false
Return
;__________________________________________________________
F2::
SendRaw This is a 5 second interruption.
Sleep, 5000
Send {Enter}
Return
;__________________________________________________________
F3::
Critical
breakLoop := true
Critical Off
return

;__________________________________________________________
F4::ExitApp

breakSleep(ms) {
	Loop % Round(ms / 100)
	{
		if breakLoop
			break		

		Sleep 100
	}
}

breakSendRaw(str) {
	for each, char in StrSplit(str)
	{
		if breakLoop
			return

		Send % "{Text}" char
		Sleep 20 ; uncomment this to simulate the typewriter effect
	}
}
this is jank. dont do this
Pepineros
Posts: 15
Joined: 16 Apr 2018, 17:26
Location: Ireland

Re: Hotkey to kill a looping thread in the same script

27 Nov 2018, 02:03

swagfag wrote:
26 Nov 2018, 23:12
this is jank. dont do this
Creative jank!
WeThotUWasAToad wrote:
26 Nov 2018, 21:59
Is there a command which will kill a looping thread contained in the same script without actually killing/closing the script?
Not that I'm aware of. If reloading is OK (which is really killing & restarting your script, so maybe this is not the solution you're looking for) you could simply use

Code: Select all

F3::reload
Reloading the current script will stop the loop.

Alternatively, instead of using loop,60 you can use the settimer command. This is usually easier to control than a loop for things that should happen at specific time intervals, like once a second in your case.
https://autohotkey.com/docs/commands/SetTimer.htm
User avatar
Scr1pter
Posts: 1069
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Hotkey to kill a looping thread in the same script

24 Dec 2018, 10:14

@Pepineros:
Reload does not kill it.
If you execute a code block and perform a reload, it will continue till the next return.

Happened to me:
https://www.autohotkey.com/boards/viewt ... 76&t=60068

Regards
Please use code tags when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602 - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.29.00

Return to “Ask For Help”

Who is online

Users browsing this forum: DRocks and 287 guests