How to interrupt a subroutine?

Get help with using AutoHotkey and its commands and hotkeys
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

How to interrupt a subroutine?

25 Feb 2019, 08:21

Hello.
I have created a long process of multiple steps which is triggered by a hotkey. It has this form:

Code: Select all

Send F2
Sleep 700
do stuff A
Sleep 700
Send F2
Sleep 700
Send F2
Sleep 700
do stuff B
Sleep 700
Send F2
etc etc etc
This pattern repeats 20 times. it is not a loop, it is a long subroutine which runs only once, with some repetitive steps.
Sometimes I need to stop the subroutine before it reaches its end.
But I dont know how to do it. I tried with another hotkey to reload the entire script. But it doesnt work consistently, sometimes the process keeps going until it ends although I triggered the reload.

So how can I do it? How can I terminate the subroutine from another hotkey before it reaches its end? preferably without having to reload the script.
Last edited by KilliK on 25 Feb 2019, 09:55, edited 1 time in total.
AviationGuy
Posts: 188
Joined: 17 Jan 2019, 10:13

Re: Abruptly stop a long series of sequential events. How?

25 Feb 2019, 08:39

Hi,
Try this. Press f1 to start and press f2 to stop the script from running.

Code: Select all

f1::
Toggle := 1
While Toggle
{
send x
sleep 1000
send x
sleep 1000
etc, etc, etc
}
return

f2:: Toggle := 0
Hope this helps.
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

Re: Abruptly stop a long series of sequential events. How?

25 Feb 2019, 09:52

I tried your suggestion, but it doesnt work. the subroutine will continue to its end, even if I change the toggle.
User avatar
Scr1pter
Posts: 1217
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to interrupt a subroutine?

25 Feb 2019, 10:43

Perhaps this command might help you:
https://autohotkey.com/docs/commands/_MaxThreadsPerHotkey.htm

The value should be 2 in your case.

And keep in mind that Send F2 does not do what you probably expect.

Also, can't you just create some condition inside of your sub routines?

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: G502 FE, Jelly Comb Touchpad - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.32.00
0x00
Posts: 87
Joined: 22 Jan 2019, 13:12

Re: How to interrupt a subroutine?

25 Feb 2019, 11:29

K,I know this is a tad NOT straight forward, but it works,so...

Code: Select all

actionList=
(
Send F2
Sleep 700
DoStuffA
Sleep 700
Send F2
Sleep 700
Send F2
Sleep 700
DoStuffB
Sleep 700
Send F2
;etc etc etc
)

ArrayFromList(arr,actionList,,"")		;single dimensional array

For k,v in arr
	( !interrupt ? (thisFunc:=StrSplit(v," ")[1]) & (thisAct:=StrSplit(v," ")[2]) & %thisFunc%(thisAct) : "")
interrupt :=false


Sleep(ms){
	Sleep % ms
}

Send(key){
	Send {%Key%}
}

DoStuffA(){
	;... stuff
	SoundBeep 1000
}

DoStuffB(){
	;... stuff
	SoundBeep 2000
}

ArrayFromList(ByRef larray, ByRef list, listDelim := "`n", lineDelim:=","){
	larray := []
	Loop, Parse, list, % listDelim
		( lineDelim && A_LoopField ? larray.Push(StrSplit(A_LoopField,lineDelim)) : larray.Push(A_LoopField) )
}

F10::MsgBox, 0x40040, %A_ScriptName%, % "Action Interrupted" . interrupt:=true
swagfag
Posts: 4090
Joined: 11 Jan 2017, 17:59

Re: How to interrupt a subroutine?

25 Feb 2019, 13:05

this can be achieved in one of several ways:
  • Reload ur currently running script
  • Run the interruptible action as a second script, parallel to ur main one
  • if using ahk_h, run the interruptible action in a separate thread
  • use a modified Sleep command that would periodically check if it should break:

    Code: Select all

    mysleep(ms) {
        static INCREMENT := 25
        
        global g_RUNNING := true
    
        Loop % Ceil(ms / INCREMENT)
        {
            Sleep INCREMENT
    
            if !g_RUNNING
                Exit
        }
    }
    
  • pass ur commands to a SetTimer invoker class, that will monitor a (not necessarily)global flag
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

Re: How to interrupt a subroutine?

26 Feb 2019, 03:10

thank you all for your help, you gave some good suggestions.
For now, I decided to break the subroutine into several groups containing the same repetitive steps and have an if statement in each one with the same condition which is toggled by another hotkey. so far it works well. thanks again for the help.

Return to “Ask For Help”

Who is online

Users browsing this forum: Elgin, Google [Bot], Kellyzkorner_NJ, Lee James, LeFunk, Netocon and 58 guests