[ERROR] SetTimer("LABEL", ...)

Discuss the future of the AutoHotkey language
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

[ERROR] SetTimer("LABEL", ...)

02 Sep 2017, 05:11

AutoHotkey 078 Perfect working.

Code: Select all

ExtensionConvert()
ExtensionConvert() {
	LABEL_ExtensionConvert:
			tooltip(A_ThisLabel " : " a++)			; action too long >>> so timer off
		SetTimer("LABEL_ExtensionConvert", "Off")	
			FileMove("c:\temp\*.S*24", "c:\temp\*.S$$")
			FileMove("c:\temp\*.K*24", "c:\temp\*.K$$")
			; run timer again
		SetTimer("LABEL_ExtensionConvert",  998)
	RETURN ;not needed	
}
AutoHotkey_2.0-a081-cad307c

Code: Select all

ExtensionConvert()
ExtensionConvert() {
	LABEL_ExtensionConvert:
			tooltip(A_ThisLabel " : " a++)			; action too long >>> so timer off
		SetTimer("LABEL_ExtensionConvert", "Off")	; Erro: Target label does not exist.
													; Specifically: LABEL_ExtensionConvert
			FileMove("c:\temp\*.S*24", "c:\temp\*.S$$")
			FileMove("c:\temp\*.K*24", "c:\temp\*.K$$")
		SetTimer("LABEL_ExtensionConvert",  998)
	RETURN
}
When first call everything OK. When SetTimer call only SetTimer() can`t see Label. A_ThisLabel is set to "invisible" label.

Code: Select all

ExtensionConvert() {
	LABEL_ExtensionConvert:
			tooltip(A_ThisLabel " : " a++)	; action too long >>> so timer off
		if A_ThisLabel	; void first call when A_ThisLabel is ""
			SetTimer(A_ThisLabel, "Off")	; Erro: Target label does not exist.
											; Specifically: LABEL_ExtensionConvert
			FileMove("c:\temp\*.S*24", "c:\temp\*.S$$")
			FileMove("c:\temp\*.K*24", "c:\temp\*.K$$")
		if A_ThisLabel	
			 SetTimer(A_ThisLabel, 998)
		else SetTimer("LABEL_ExtensionConvert",  998)
	RETURN
}
The same.
There no scope problem with label due to settimer calls runs and A_ThisLabel is set correctly.
AHKv2.0 alpha forever.
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

[ERROR] SetTimer("LABEL", ...)

02 Sep 2017, 05:48

Code: Select all

ExtensionConvert() {
LABEL_ExtensionConvert:
	label:= A_ThisLabel? "": "LABEL_ExtensionConvert" ; second call with "unckown" label ("")
		tooltip(A_ThisLabel " | " label " : " a++) 	  ; visual debug line
	SetTimer(label, "Off")	
		FileMove("c:\temp\*.S*24", "c:\temp\*.S$$")
		FileMove("c:\temp\*.K*24", "c:\temp\*.K$$")
	SetTimer(label,  998)
RETURN
}
Working but ugly.
If A_ThisLabel is set SetTimer must use "" as label.

Edit after test:

Code: Select all

ExtensionConvert() {
	label:= "LABEL_ExtensionConvert" ; set label for first 
LABEL_ExtensionConvert:
	SetTimer(label, "Off") ; when SetTimer call label is ""
		FileMove("c:\temp\*.S*24", "c:\temp\*.S$$")
		FileMove("c:\temp\*.K*24", "c:\temp\*.K$$")
	SetTimer(label,  998)
RETURN
}
AHKv2.0 alpha forever.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: [ERROR] SetTimer("LABEL", ...)

02 Sep 2017, 06:40

How about this?:

Code: Select all

ToggleTimer(1)
Sleep(10000)
ToggleTimer(0)

ToggleTimer(Toggle)
{
	STATIC TheTimer := Func("TestTimer")
	SetTimer(TheTimer, Toggle ? "1000" : "OFF")
	If (!Toggle)
		ToolTip()
}

TestTimer()
{
	ToolTip(A_Now)
}
Uses function or method instead of label.
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.
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [ERROR] SetTimer("LABEL", ...)

02 Sep 2017, 08:10

Idea is to separate function body to call action and timer call action.

Code: Select all

Timed(200)	 		; run section (1) and (2)
msgBox("Change.")	; while we wait run just (2)
Timed(500) 			; run section (1) and (2)
msgBox("Stop.")		; while we wait run just (2) 
Timed(   0) 		; run section (1) and (2)

Timed(__t) {
; function call section --------------------------- (1)
	static time:= 1000
	label:= "LABEL_"
	time:= __t >= 0? __t: time 
; -----------------------------------------------------	
LABEL_:
; timer call section ============================== (2)
	SetTimer(label, "Off")
		 ; long action
		 msgBox("label:= [" label "] time:= " time " count:= " a++)
		 ; ToolTip("label:= [" label "] time:= " time " count:= " a++) 
		 
	if time ; time == 0 stop	
		 SetTimer(label,  time) 
	else ToolTip	
RETURN ; ==============================================
}
Esc::ExitApp
https://autohotkey.com/boards/viewtopic ... 488#p10488
This way function encapsulate timer itself and timer execute just part of function code.

Code: Select all

CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"
timedFunction()

timedFunction() {
	static x, y
	SetTimer(label_1:= "LABEL_1", 100)
	SetTimer(label_2:= "LABEL_2", 200)
	SetTimer(label_3:= "LABEL_3",  50)
RETURN	
LABEL_1:
	toolTip(a++, x+15, y+37, 10)
RETURN
LABEL_2:
	toolTip(b++, x+15, y+59, 11)
RETURN
LABEL_3:
	MouseGetPos(x, y)
	toolTip(x ":" y, x+15, y+15, 12)
RETURN	
}

Esc::ExitApp
Just for fun.
Last edited by _3D_ on 02 Sep 2017, 08:54, edited 1 time in total.
AHKv2.0 alpha forever.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [ERROR] SetTimer("LABEL", ...)

02 Sep 2017, 08:25

For convenience this could be fixed by adding this code here.

Code: Select all

	if (g->CurrentLabel && !_tcsicmp(g->CurrentLabel->mName, aLabelName))
		return g->CurrentLabel;
EDIT:
I have created a pull request.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [ERROR] SetTimer("LABEL", ...)

02 Sep 2017, 17:29

The function is not called, so no (other) local variables or labels are accessible dynamically. HotKeyIt's "fix" adds an inconsistency to the language, making A_ThisLabel accessible but not any other local labels or variables. My preference is to leave it as is.

If you want encapsulation, you can use an object or bound function.
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [ERROR] SetTimer("LABEL", ...)

03 Sep 2017, 06:55

lexikos wrote:The function is not called, so no (other) local variables or labels are accessible dynamically. HotKeyIt's "fix" adds an inconsistency to the language, making A_ThisLabel accessible but not any other local labels or variables. My preference is to leave it as is.

If you want encapsulation, you can use an object or bound function.
Correct. Code enclosed in LABEL/RETURN that timer call is local for itself when timer call. And it CAN`T see other local variables and labels and CAN`T manipulate it. Problem is: at the next line after LABEL A_ThisLabel is not set (mean == "").

Code: Select all

fun(t1, t2) {
	static a:= 10
	Gosub("LB_2") 		; set A_ThisLabel for first use
 	;lb_1:= "LB_1"		; (1) working set 1
	Goto(lb_1:= "LB_1") ; (2) working set 2
	LB_1:
		;  <<< if no goTO or goSUB A_ThisLabel is ""
		;setTimer(lb_1, "Off")	; (1)
		setTimer(, "Off")		; (2)
		tooltip("++: " a++, 100, 100,10)
		;setTimer(lb_1, t1? t1: "On")	; (1)
		setTimer(, t1? t1: "On")		; (2)
	RETURN
	LB_2:
		setTimer(, "Off")
		tooltip("--: " (--a), 100, 122, 11)
		setTimer(,  t2? t2: "On")
	RETURN
}

fun(200, 2000)
Esc::ExitApp
Gosub init LB_2 and in case (2) Goto init LB_1 so settimer can be used SetTimer(, value). In case (1) no Goto and must use SetTimer(A_ThisLabel? "": "LB_1") that is the same to using local variable (that is "" when timer call and "LB_1" in function call).

Code: Select all

nul() {
	LABEL:
		msgBox(A_ThisLabel) ; ""
}

Code: Select all

nul() {
	Goto("LABEL")
	LABEL:
		msgBox(A_ThisLabel) ; "LABEL"
}
Timer work perfect but A_ThisLabel must be set forced.
Edited:
BTW

Code: Select all

LABEL 				  ; missing :
	msgBox("No Run.") ; <<< dead code
return
Script can`t run and no any Error.

Code: Select all

123.2 ;No object to invoke. ??? var begin [a-z_]
AHKv2.0 alpha forever.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 29 guests