SetTimer as metod - Ex4 Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Albireo
Posts: 832
Joined: 16 Oct 2013, 13:53

SetTimer as metod - Ex4

03 Dec 2018, 09:01

Hi!
Was inspired when I saw the example.: Using a method as the timer subroutine ( SetTimer ).

Code: Select all

; Example #4: Using a method as the timer subroutine.

counter := new SecondCounter
counter.Start()
Sleep 5000
counter.Stop()
Sleep 2000

; An example class for counting the seconds...
class SecondCounter {
    __New() {
        this.interval := 1000
        this.count := 0
        ; Tick() has an implicit parameter "this" which is a reference to
        ; the object, so we need to create a function which encapsulates
        ; "this" and the method to call:
        this.timer := ObjBindMethod(this, "Tick")
    }
    Start() {
        ; Known limitation: SetTimer requires a plain variable reference.
        timer := this.timer
        SetTimer % timer, % this.interval
        ToolTip % "Counter started"
    }
    Stop() {
        ; To turn off the timer, we must pass the same object as before:
        timer := this.timer
        SetTimer % timer, Off
        ToolTip % "Counter stopped at " this.count
    }
    ; In this example, the timer calls this method:
    Tick() {
        ToolTip % ++this.count
    }
}
But I do not understand how I can use this to two of my wishes.

1. From the example - is it difficult to get this.count in a variable?
(something like this)

Code: Select all

counter := new SecondCounter
counter.Start()
Sleep 3000
TimeHold := counter.Tick()
MsgBox % TimeHold
Sleep 3000
counter.Stop()
Sleep 2000
ExitApp
2. I want to break a loop after eg 3 seconds
(something like this)

Code: Select all

counter := new SecondCounter
counter.Start()
Loop
{ 	; some instructions
	;
	; This compare should be somewhere else 
	; If counter.Tick() > 3
	;	Break
	;
	; some instructions
}
ExitApp
John
Posts: 75
Joined: 10 Apr 2014, 08:59

Re: SetTimer as metod - Ex4  Topic is solved

03 Dec 2018, 09:38

Both of your goals could be achieved with a simple

Code: Select all

start_time := A_TickCount
time_out := 3000
loop {
	ticks := a_tickcount - start_time
	tooltip % ticks
	if (ticks >= time_out)
		break
}
exitapp
But if you want to use that class, then I'd do something like this

Code: Select all

; Example #4: Using a method as the timer subroutine.

counter := new SecondCounter()
counter.time_out := 3000
counter.Start()
loop {
	if (counter.state == 2)
		counter.Stop()
	tooltip, % counter.count "|" counter.state ,0,0,2
}	
MsgBox loop done

class SecondCounter {
    __New() {
        this.interval := 1000
        this.count := 0
		this.state := 0 ;0 = off, 1 = running, 2 = timeout
        this.timer := ObjBindMethod(this, "Tick")
    }
    Start() {
		this.state := 1
        timer := this.timer
        SetTimer % timer, % this.interval
    }
    Stop() {
		this.state := 0
        timer := this.timer
        SetTimer % timer, Off
    }
    Tick() {
		++this.count
		if (this.time_out && (this.count*this.interval) >= this.time_out)
			this.state := 2
    }
}

rshift::Exitapp
Setting the state of the timer so in your loop you could make the timeout conditional and dependent on the logics of the if check. Optionally you could probably do another ObjBindMethod(this,"TimeOut") and set a timer for it too and then time it out like that, but that feels a bit redundant.

Also your last example would work too with a tiny change

Code: Select all

if (counter.count >= 3) {
		counter.Stop()
		break
	}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 173 guests