Multiple timer only runs once

In trying to set a desktop app (Xojo 2018 r2) to quit after a specified period of time, I have set a timer in the open event of the application, like so:

TimeOutTimer = New Timer AddHandler TimeOutTimer.Action, AddressOf Timer_Quit TimeOutTimer.Period = 2000 //2 seconds TimeOutTimer.Mode = Timer.ModeMultiple TimeOutTimer.Enabled = True

[code]function TimerQuit (sender as Timer)
dim idleTime as double = UserIdleSeconds
dim myLimit as Integer = 30*60 //30 minutes (in seconds)

 Beep  //test if the method runs

 if idleTime >= myLimit then
   sender.Period = 2000 // Because, potentially, of dialogs
   Quit
 else
   sender.Period = ( myLimit - idleTime ) * 1000
 end if

end function[/code]

The TimerQuit method seems to only run (beeps) once.

This seems to work the same way whether I use Timer (classic) or Xojo.Core.Timer.

Not sure why this timer stops after the first run.

TimeOutTimer = New Timer

If this is declared in the Open event, then at the end of the Open event, this object will become nil

Make TimeOutTimer a property of the app, and it will likely work.

I’m sorry, I neglected to say that TimeOutTimer is a property of the app. Must be something else going on.

Hmmm. I just heard a beep after a long period of time. Could my interval somehow not be 2 seconds?

You’ve changed your interval immediately after the first time the timer fired. The timer will fire for the first time after 2 seconds and then it will immediately change its interval (period) in the TimerQuit function. When it’s fired that first time, the code resets the interval to 1798000 milliseconds.

sender.Period = ( myLimit - idleTime ) * 1000

(1800 - 2) * 1000 = 1798000 miliseconds or almost half an hour.

Thanks. I really wasn’t clear what “sender” referred to as a parameter within a timer method.

Is “sender” a new timer within the TimeOutTimer method, or something related to the timer that is currently running?

[quote=409707:@David Schlam]Thanks. I really wasn’t clear what “sender” referred to as a parameter within a timer method.

Is “sender” a new timer within the TimeOutTimer method, or something related to the timer that is currently running?[/quote]
The TimerQuit method handles the firing of your TimeOutTimer. The AddHandler line says “whenever the action event fires for me (the timer), I want a method called TimerQuit to handle it”. So, inside TimerQuit, sender refers to that exact same timer.

For a quick fix, take out the else statement and the timer will keep firing every 2 seconds, always checking if the idleTime has been reached. Of course, this is inefficient because the first time that the app could quit is 30 mins away…

Is this a typo?

AddHandler TimeOutTimer.Action, AddressOf Timer_Quit

function TimerQuit (sender as Timer)

[quote=409717:@Tim Streater]
Is this a typo?

AddHandler TimeOutTimer.Action, AddressOf Timer_Quit
function TimerQuit (sender as Timer)[/quote]
Probably, since I wasn’t sure how to copy a function and its parameters so I just typed that out.

Do you not have to put a parameter of “sender” in the Timer_Quit method?

True, the first instance can’t be less than 30 minutes away so the first instance of the timer should occur in 30 minutes and then run (myLimit - idleTime) seconds after that. I was trying to test the method with a shorter start time and (as things would have it) got confused looking at old code. I usually try to document everything but could have done a better job here.

Also beware that myTimer.Reset sets it entirely back to it’s default settings, not just it’s clock. So if it was Mode.Single and you changed it in code to Mode.multiple, then a Reset will set it back to Mode.Single.