Quick Question About Timers

Just wondering which is better or it doesn’t matter. In coding the Action for a Timer should I call Me.Enable = False first then have the timer code run or should I call it last after everything the Timer is supposed to do has run? Just wondering.

I would disable the time as the first step in the timer action, this way you are insured that the timer will not fire again before the action processing has completed…

UNLESS, you know two things

  • the action processing will NEVER exceed the period of the timer
  • the action MUST be run at specific intervals (disabling the timer will cause the intervals to be less consistent, as the processing time now becomes part of that interval)

I don’t use the Enable I set the mode to ModeOff. It’s more explicit, in my opinion.

me.mode = timer.ModeOff

I also put it first line of the event simply because a short period might have it fire again before the method is done.

Thanks for the clarifications.

Another clarification. The Action cannot overlap as it always runs in the main thread.

while that is true… doesn’t the timer firing post a message to the queue, and that message is executed at the next available timeslot?
Meaning it is possible for the action to fire heel to toe with no delay if messages were queued up while the action was being processed (ie. the proceesing exceeded the timer period, and the time was left running)

I had the same question as Dave. I treated timers as interrupts.

I just tested this with a 1/2 second multimode timer that takes 10 seconds to run it’s event then turns itself off at the end. Only 1 event fires. So it must be removed from the queue in the Me.Mode = 0 call before it can fire again.

Will, when does it fire next if you don’t turn it off? Heel to toe or after a delay?

I think it will be heel to toe.

Heel to toe. I just checked.

I don’t mean to sound like a noob but what does Heel to Toe mean. I know what it means as a drummer but not in coding.

In my test the Timer is set to fire every 1/2 second but the Action event is taking 10 seconds to complete. As soon as the event finishes the next one fires (heel to toe) because it’s well past when it wanted to fire, effectively giving a period of 10 seconds.

I don’t mean to sound like an idiot but I have no idea what you just said.

What part don’t you understand?

Maybe this will make more sense. The Timer is setup to fire every 1/2 second but when it does the event code takes 10 seconds to execute. During that 10 seconds the timer doesn’t fire again, the event code has to finish before firing again. When it does finally finish the next firing happens right away (heel to toe). It was scheduled to fire the second Action 9.5 seconds ago but couldn’t because code is running. The Actions fire sequentially.

[code]Sub Action() //Timer1, Period 500 = 1/2 sec, initially off

System.DebugLog(“started”)

dim stop As integer = Ticks + 60 * 10 //get stop time 10 seconds from now

while Ticks < stop //spin here for 10 seconds
wend

System.DebugLog(“ended”)

End Sub

Sub Action() //PushButton1
Timer1.Mode = 2 //start running the timer
End Sub[/code]

Now it makes more sense. You can’t fire the same Timer until that Timer finishes executing its current action. I thought you were saying that 2 Timers can’t be active at the same time.

Calling Reset at the end of the Action should create the delay, if that’s what you want. Or set the Mode again, either way.

To underscore the matter, Timers fire during idle time. That’s why their timing is not guaranteed.

2 Timers can be active at the same time, but if one of them takes 10 seconds to finish it’s Action event, it will block both timers (not to mention the entire UI).

Who said anything about a delay? It was said that a Timer couldn’t fire if one was already in play. I wasn’t sure if that meant you couldn’t run 2 Timers simultaneously or you couldn’t fire the same Timer a second time while the first run finishes. Let me get this straight, there is a 10 second rule with Timers?

My question about timer was that I wasn’t sure if turning a running Timer off as the first line of Action code would stop the Timer immediately or it would finish the remaining code before fully stopping. I just wanted to know what was the proper way of doing things so that I don’t get unintended actions.

In my TCG Game I’m working on I have a series of Timers that mostly handle Animations during the duel when a new phase of a player’s turn begins as well as do appropriate actions setting up the new phase.

Once the Timer Action has started, it’s like any other method. It can set properties like the Timer.Mode or perform other actions and will only stop once it’s run its course. As such, there is no difference in this code:

[code]me.Mode = Timer.ModeOff

//
// A lotta code
//[/code]

and

//
// A lotta code that doesn't Return from the method
//

me.Mode = Timer.ModeOff

Edit: fixed code block