Question about __Delete() behavior in class with timer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Question about __Delete() behavior in class with timer

10 Nov 2016, 12:24

Hi Folks,

I would appreciate it if you could take a look at the following code, which is a slightly modified version of this example SetTimer.htm#ExampleClass in the documentation. I added the __Delete() meta-function, but it's not behaving as I would expect it to.

Thank you.

Code: Select all

counter := new SecondCounter
counter.Start()
Sleep 5000
counter.Stop()
Sleep 2000
counter =   ; I would expect a ToolTip to show up as a result of this operation
Sleep 2000
Return

; From https://autohotkey.com/docs/commands/SetTimer.htm#ExampleClass

class SecondCounter {
   __New() {
     this.interval := 1000
     this.count := 0
     this.timer := ObjBindMethod(this, "Tick")
   }
   Start() {
      timer := this.timer
      SetTimer % timer, % this.interval
      ToolTip % "Counter started"
   }
   Stop() {
      timer := this.timer
      SetTimer % timer, Off
      ToolTip % "Counter stopped at " this.count
   }
   Tick() {
      ToolTip % ++this.count
   }
   ; This function is the only addition to the class
   __Delete() {
      ToolTip Deleting object ...
   }
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Question about __Delete() behavior in class with timer

10 Nov 2016, 13:31

objects wrote:__Delete is not called for any object which has the key "__Class". Class objects have this key by default.
See, Objects.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Question about __Delete() behavior in class with timer  Topic is solved

10 Nov 2016, 13:57

Helgef wrote:
objects wrote:__Delete is not called for any object which has the key "__Class". Class objects have this key by default.
See, Objects.
I don't think that applies to this question. The "Class object" in the above script is SecondCounter (The prototype object from which instances are derived). __Delete should still be called for instances of the class.

The reason __Delete is not called seems to be because the bound method is not released. In order to release the object it seems that you need to: 1.) Delete the object reference stored in this.timer and 2.) call SetTimer % timer, Delete.

Code: Select all

counter := new SecondCounter()
counter.Start()
Sleep 2000
counter.Stop()
Sleep 2000
counter.DeleteTimer()  ; <----
counter := ""
Sleep 2000
Return

; From https://autohotkey.com/docs/commands/SetTimer.htm#ExampleClass
class SecondCounter {
    __New() {
        this.interval := 1000
        this.count := 0
        this.timer := ObjBindMethod(this, "Tick")
    }
    Start() {
        timer := this.timer
        SetTimer % timer, % this.interval
        ToolTip % "Counter started"
    }
    Stop() {
        timer := this.timer
        SetTimer % timer, Off
        ToolTip % "Counter stopped at " this.count
    }
    Tick() {
        ToolTip % ++this.count
    }
    DeleteTimer() {                 ; <----
        timer := this.timer
        this.timer := ""            ; Free reference to bound method.
        SetTimer % timer, Delete    ; Delete the timer. Turning off a timer does not release the object.
    }
    ; This function is the only addition to the class
    __Delete() {
        ToolTip Deleting object ...
    }
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Question about __Delete() behavior in class with timer

10 Nov 2016, 14:50

kon wrote:

Code: Select all

SetTimer % timer, Delete  
That I missed, thank you.
My next mistake was that, since

Code: Select all

counter := new SecondCounter()
msgbox, % IsObject(counter) "`n" counter["__class"]
yields 1 and SecondCounter, I assumed

Code: Select all

Msgbox, % counter.HasKey("__Class")
would be true, but apparently, it is 0
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Question about __Delete() behavior in class with timer

10 Nov 2016, 14:57

Helgef wrote:My next mistake was that, since

Code: Select all

counter := new SecondCounter()
msgbox, % IsObject(counter) "`n" counter["__class"]
yields 1 and SecondCounter, I assumed

Code: Select all

Msgbox, % counter.HasKey("__Class")
would be true, but apparently, it is 0
The reason counter["__class"] works (returns "SecondCounter") is because when the key isn't found in the instantiated object (counter), AHK automatically looks for the key in the base object (SecondCounter).
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Question about __Delete() behavior in class with timer

10 Nov 2016, 15:01

Thanks for the clarification, good to keep in mind.
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Question about __Delete() behavior in class with timer

10 Nov 2016, 17:45

Thank you, kon. I appreciate the insight and the implementation. I wonder if your solution should be implemented in the example in the documentation so that others can learn from it as well.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Question about __Delete() behavior in class with timer

11 Nov 2016, 17:53

Based on your insight and the additional notes at the end of the SetTimer page, I came up with the following simpler version of the class:

Code: Select all

counter := new SecondCounter()
Loop 2
{
   counter.Start()
   Sleep 2000
   counter.Stop()
   Sleep 2000
}
counter := ""
Sleep 2000
Return

class SecondCounter {
    __New() {
        this.interval := 1000
        this.count := 0
    }
    Start() {
        SetTimer % this, % this.interval
        ToolTip % "Counter started"
    }
    Stop() {
        SetTimer % this, Delete                       ; Delete the timer instead of turning it off
        ToolTip % "Counter stopped at " this.count
    }
    Call() {
        ToolTip % ++this.count
    }
    __Delete() {
        ToolTip Deleting object ...
    }
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Ask for Help (v1)”

Who is online

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