AHK V2 Bug: Pause toggle not working Topic is solved

Discuss the future of the AutoHotkey language
SAbboushi
Posts: 142
Joined: 08 Dec 2014, 22:13

AHK V2 Bug: Pause toggle not working

04 Nov 2018, 16:12

Per documentation:
#p::Pause ; Pressing Win+P once will pause the script. Pressing it again will unpause.

Code: Select all

#p::Pause
Pauses the thread but won't unpause when I press #p again.
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AHK V2 Bug: Pause toggle not working  Topic is solved

04 Nov 2018, 21:37

This is expected. The hotkey cannot run a second thread until the first thread exits. It does not exit because it is paused.

v1 had special cases for certain types of actions, such as Pause bypassing #MaxThreadsPerHotkey. This is not done anymore since the hotkey's action is just a function call like any other.

Just pause the underlying thread, not the current thread.
Pause(, true)
Ursi
Posts: 18
Joined: 16 Oct 2018, 14:11
GitHub: Ursi

Re: AHK V2 Bug: Pause toggle not working

05 Nov 2018, 13:22

I was just having this problem and just posted in the help section about it before I saw this. The documentation is pretty misleading and should probably be changed.
SAbboushi
Posts: 142
Joined: 08 Dec 2014, 22:13

Re: AHK V2 Bug: Pause toggle not working

05 Nov 2018, 16:10

Understanding Pause was way more complicated than I expected (due to my ignorance). It took a bit of testing on my part.

Posting my test code in case it is of use to others who were as confused as I was. My notes relate to a debug session in SciTE4AutoHotkey:

Code: Select all

	3 ways to pause/unpause:

		1) #p:: using "Pause()"; have to manually unpause
		
		2) #q:: using "#MaxThreadsPerHotkey 2" and pause(); but I guess this will affect all other
					hotkeys until I reset it to 1 after unpause...
					
		3) #r:: using "Pause( ,true)": (I believe) this pauses the thread that I just interrupted,
			so I suspect if I have multiple active threads, I could end up pausing the wrong one.
			I guess if that becomes a problem, maybe I can read the previous entry in the callstack
			and if it's not the correct thread, sleep and then try again?

*/
; 

KeyHistory
#p::
{
	Pause() ; After pressing "#p", refresh the KeyHistory window:
			; 	Paused threads: 1 of 1 (1 layers)
			
			; Since default for "#MaxThreadsPerHotkey" is 1, pressing the hotkey again ("#p")
			;	has no effect because this thread is paused.
			
			; 1) Put a breakpoint on "x:=1".
			; 2) Manually uncheck "Pause" in this script's systray icon
	KeyHistory
	Sleep 100
	x:=1	; 3) When breakpoint is triggered, KeyHistory should show:
			; 	Paused threads: 0 of 1 (0 layers)
	return
			
}	; Pressing Win+P once will pause the script. Pressing it again will unpause.


#MaxThreadsPerHotkey 2
#q::
{				
	Pause() 	; After pressing "#q", refresh the KeyHistory window:
				; 	Paused threads: 1 of 1 (1 layers)
				
				; Press CTRL/h for "Hotkeys and their methods" window:
				;	Type	Off?	Level	Running		Name
				;	-------------------------------------------------------------------
				;	k-hook								#p
				;	k-hook					1			#q
				
				; Single step in debugger:
				
				; Since "#MaxThreadsPerHotkey" is now 2, pressing the hotkey again ("#q")
				;	will launch a new thread, interrupting the first paused thread:
			
					; 1) Press the hotkey again ("#q")
	KeyHistory
	x:=2			; 2) KeyHistory should show:
					;	Interrupted threads: 1 (preempted: ...
					;	Paused threads: 0 of 2 (1 layers)
					
					; If I could press CTRL/h for "Hotkeys and their methods" window:
					
					;	Type	Off?	Level	Running		Name
					;	-------------------------------------------------------------------
					;	k-hook								#p
					;	k-hook					2			#q
			
					; Examine stack to see 2 x "#q thread"
					
					; After the 2nd thread finishes, the original "#q" (paused but then unpaused
					;	as a result of the second "#q" thread launching) will continue to execute
					;	on the next statement after "Pause()"
	return
}

#MaxThreadsPerHotkey 1

#r::
{
	Pause(, true) 	; "If there is no thread beneath the current thread (SA: i.e. another thread
					;	wasn't interrupted because this is the only running thread), the script 
					;	itself is paused, which prevents timers from running (this effect is the 
					;	same as having used the menu item "Pause Script" while the script has no 
					;	threads)."
    
					; After pressing "#r", refresh the KeyHistory window:
					; 	Paused threads: 0 of 0 (0 layers)
					
					; Note also that the systray icon is still green because this thread will 
					;	continue until it exits.
					
					; Since there were no underlying threads, the script itself is paused, but this
					;	thread finishes executing first, so there are no paused threads:
					;		"Paused threads: 0"
					
					; If I could press CTRL/h for "Hotkeys and their methods" window:
					
					;	Type	Off?	Level	Running		Name
					;	-------------------------------------------------------------------
					;	k-hook								#p
					;	k-hook								#q
					;	k-hook								#r
					
					; Note: "If I could press..." - I can only do that if I have MsgBox after
					;	"KeyHistory" line below.  But MsgBox allows other threads to continue
					;	 or interrupt, so using "x := 3" with breakpoint
					
				
					; 1) Press the hotkey again ("#r")
	KeyHistory
	ListHotkeys
	x := 3
	; MsgBox 3		; 2) KeyHistory should show:
					;	Paused threads: 0 of 1 (0 layers)
					
					;	The original "#r" thread
					
					; Press CTRL/h for "Hotkeys and their methods" window:
					;	Type	Off?	Level	Running		Name
					;	-------------------------------------------------------------------
					;	k-hook								#p
					;	k-hook								#q
					;	k-hook					1			#r
					
	return			; script is paused after "return"
}

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: Google [Bot] and 15 guests