[SOLVED] How can I stop a timer with a Func.Bind(param)?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
slur
Posts: 10
Joined: 13 Jun 2014, 07:05

[SOLVED] How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 03:37

I know it's not in official document but since I find some users using SetTimer like this here and there, I gave it a try

Code: Select all

KeyHistory
return

!F12:: ExitApp

F1::
F_TimerBound := Func("TimerTest").Bind(A_Now)
SetTimer, %F_TimerBound%
return

TimerTest(_p) {
	Global F_TimerBound

	SetTimer, %F_TimerBound%, Off
	;SetTimer, %F_TimerBound%, Delete
	KeyHistory
	ToolTip, %_p%
}
But I can't find a way to stop a timer
Every time I press F1 Enabled Timers count just increases and that's where I'm stuck
Some hint please?
Last edited by slur on 11 Nov 2015, 21:55, edited 1 time in total.
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 06:12

What do you mean by "not in official document"? Every feature you've used in this script is documented.

SetTimer, %F_TimerBound%, Delete is the correct thing to do. Just uncomment that line.

The counter shown in KeyHistory does not decrease when you delete the timer. This is a bug; the timer really is deleted.

Since the timer is still running, it isn't actually deleted until the function returns. If you call KeyHistory before the function returns, it will try to list the names of all enabled timers, and it will crash if one of them has been deleted. This is a bug. SetTimer, %F_TimerBound%, Off is sufficient to work around this problem.

You might think to work around this problem by setting another timer to call KeyHistory after the function returns. However, if you delete the most recently created timer and there is still at least one timer (i.e. the KeyHistory timer), any other timers you try to create will never execute. This is a bug.

These bugs have been fixed in v1.1.22.09.
slur
Posts: 10
Joined: 13 Jun 2014, 07:05

Re: How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 08:59

Ooops, I think I was being too cautious, maybe a coward when reading the document
If not a valid label name, this parameter can be the name of a function, or a single variable reference containing a function object. For example, SetTimer %funcobj%, 1000 or SetTimer % funcobj, 1000. Other expressions which return objects are currently unsupported.
I thought passing boundfunc object is not yet officially supported. :headwall:
Partly because if I remove bind(param) part, there was no such glitches mentioned.
So the underlined part was maybe just about dereferencing, I guess?

That being said, when using bind method, Timer name (as seen in KeyHistory window) would be just Object, which when unbound would be function name.

Anyways, thank you for the kind answer. I'll be looking forward to upcoming v1.1.22.09 with my fingers crossed.
just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 11:58

Does this mean the only way to access to the BoundFunc object / the current timer instance within the timer is a global variable?
Coco-guest

Re: How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 12:44

just me wrote:Does this mean the only way to access to the BoundFunc object / the current timer instance within the timer is a global variable?
Not necessarily:

Code: Select all

obj := {}
timer := Func("f").Bind(obj)
obj.Timer := timer
SetTimer, %timer%, -1

Sleep, 2000
return

f(arg, args*)
{
	timer := arg.Delete("Timer")
	SetTimer, %timer%, Delete

	MsgBox, %A_ThisFunc%
}
just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 16:01

Hi coco! Do you think that your approach is more than a work-around for a missing part in the current implementation?
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: How can I stop a timer with a Func.Bind(param)?

11 Nov 2015, 16:17

slur wrote:I thought passing boundfunc object is not yet officially supported. :headwall:
Partly because if I remove bind(param) part, there was no such glitches mentioned.
If you remove that, you will be passing the same object to SetTimer, and it will set the same timer each time, not create a new one. It would be the same if you pass the same bound function repeatedly. You could encounter the same bugs by using multiple functions.
So the underlined part was maybe just about dereferencing, I guess?
Read the whole paragraph. It says to pass "a single variable reference containing a function object". That's what you did. See also Function Objects. An example of "other expressions" which aren't supported is SetTimer % Func("TimerTest").Bind(A_Now), because this is clearly not a single variable reference.
I'll be looking forward to upcoming v1.1.22.09 with my fingers crossed.
FYI, it's not "upcoming". It was already available at the time of my previous post.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Draken, oktavimark, Spawnova, william_ahk and 261 guests