AddressOf / RemoveHandler question

It seems to be advised you need to remove a handler when initiating a Timer.

    [code]mTimer = New Timer
    mTimer.Period=250
    mTimer.Enabled = true
    mTimer.mode=2
    AddHandler mTimer.Action, AddressOf TimerActionMethode

    RemoveHandler mTimer.Action, AddressOf TimerActionMethode[/code]

But what about when using CallLater? Is it automatically removed/released?

xojo.core.Timer.CallLater 25, AddressOf TimerActionMethode

You can use

WeakAddressOf

instead of AddressOf. Using WeakAddressOf you don’t need to use RemoveHandler! It automatically removes.

I would assume so since that type of Timer is “single-use”.

In your own Timer, you should use WeakAddressOf where the method belongs to an instance of a class and make sure the Timer is torn down properly in the class’ Destructor.

Matin, that’s not exactly true. WeakAddressOf does not increment the reference count on the object to which the method belongs, but that’s not the same as removing it.

Ok, i thought because @Eugene Dakin wrote in his Thread ans Timer Book

You do not use AddHandler when using Xojo.Core.Timer.Calllater, so no need to remove handlers. You might want to test if the object the method belongs to still exists, anyway.

Interesting, I didn’t think it worked that way but I’m not privy to implementation details either. @Eugene Dakin, how certain of this are you?

Having asked that, there is no downside to removing the handler yourself in any case, and it’s probably good practice.

That’s weird, I am not receiving emails from the forum and I just happen to look at this question.

I’ll look up the information once I am at my development computer tonight.

Hello Everyone,

Yes, you can use both a AddressOf-RemoveHandler or a WeakAddressOf in a Timer.

I have had a few people send emails to me in the past with troubled timer code, and the reason why I suggest using an AddressOf-RemoveHandler pair is that the coder has absolute control over when the AddressOf is initiated and removed and its use is explicit. WeakAddressOf removal is implicit - meaning it is understood that at a given portion of the code the RemoveHandler will be automatically called. If your an experienced programmer and have used it a lot, then there isn’t a concern. If you are starting to use handlers, then I would suggest using the pair.

@Will Shank has an example at the following thread:
AddressOf vs WeakAddressOf

In my humble opinion, using AddressOf-RemoveHandler makes it clearer when the handler is removed. I personally prefer using code that is explicit, as it takes the guess out of it.

[quote=361859:@Eugene Dakin]
I have had a few people send emails to me in the past with troubled timer code, and the reason why I suggest using an AddressOf-RemoveHandler pair is that the coder has absolute control over when the AddressOf is initiated and removed and its use is explicit. WeakAddressOf removal is implicit - meaning it is understood that at a given portion of the code the RemoveHandler will be automatically called. [/quote]
This is incorrect

Using Weak Address of does NOT automatically remove the handler
However IF the target of the weakaddress of is destroyed the handler may refer to something that is now nil
But the handler is still in place and you could then get a nil object exception because the target of the handler no longer exists

Destroying the object that you added handlers to with AddHandler, in this case the timer, would remove the handlers