Timer_Tick event

C

Curt Gough

I have a question about the Timer_Tick event. If I have a timer set to
"tick" every 60 seconds, and the code that is run when the event is raised
takes longer than 60 seconds to run, will the even reset, or will it wait
until my code is completed to raise the Tick event? In other words, when the
60 seconds have elapsed, will VB halt my code and re-raise the Timer_Tick
event?

TIA
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Curt said:
I have a question about the Timer_Tick event. If I have a timer set to
"tick" every 60 seconds, and the code that is run when the event is raised
takes longer than 60 seconds to run, will the even reset, or will it wait
until my code is completed to raise the Tick event? In other words, when the
60 seconds have elapsed, will VB halt my code and re-raise the Timer_Tick
event?

TIA

The timer puts a message in the message queue for your program, and when
your lengthy operation ends and the message handling resumes, your event
will be raised.

If you want a timer that does not wait for the message handling, use a
timer from the System.Threading or System.Timers namespaces. They
execute the event in a separate thread.
 
O

\(O\)enone

Curt said:
I have a question about the Timer_Tick event. If I have a timer set to
"tick" every 60 seconds, and the code that is run when the event is
raised takes longer than 60 seconds to run, will the even reset, or
will it wait until my code is completed to raise the Tick event?

Wouldn't it have been quicker to try it than to write this message? :)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

(O)enone said:
Wouldn't it have been quicker to try it than to write this message? :)

Of course it would, but that would not have answered the implied
follow-up question "why". :)
 
C

Chris Dunaway

I have a question about the Timer_Tick event. If I have a timer set to
"tick" every 60 seconds, and the code that is run when the event is raised
takes longer than 60 seconds to run, will the even reset, or will it wait
until my code is completed to raise the Tick event? In other words, when the
60 seconds have elapsed, will VB halt my code and re-raise the Timer_Tick
event?

TIA

--
Curt Gough
(e-mail address removed)

Remove 123 from above address to mail directly

The timer will fire again after the 60 seconds are up, depending on
whether or not the message queue gets processed. If your timer tick
event handler is not re-entrant, then that could pose a problem.

If you're concerned about that, then disable the timer as the first
instruction in your tick event handler and then re-enable it when you
are done or lengthen the timer interval.

Chris
 
C

Curt Gough

The "why" is that I have a program that will send a pages to pagers based on
a random schedule that I generate. I check the database every 60 seconds
using the Timer_Tick event to see if there is a page to send that minute.
Sometimes there are multiple pages to be sent and I wanted to be sure that
all would be sent before the timer ticked again. Thanks for your answers!
 
C

Chris Dunaway

The "why" is that I have a program that will send a pages to pagers based on
a random schedule that I generate. I check the database every 60 seconds
using the Timer_Tick event to see if there is a page to send that minute.
Sometimes there are multiple pages to be sent and I wanted to be sure that
all would be sent before the timer ticked again. Thanks for your answers!

A good way around that is to insert your numbers to be paged in a
queue. The timer can run every 60 seconds and insert any numbers it
finds into the queue. Then have a second thread actually processing
the queue and making the pages. Then you don't have to worry about
the timer. It can fire as often as it needs to.

Chris
 
C

Curt Gough

Thanks, Chris. That is a good suggestion. Sometimes the hardest part about
programming is just knowing what you can do.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Curt said:
The "why" is that I have a program that will send a pages to pagers based on
a random schedule that I generate. I check the database every 60 seconds
using the Timer_Tick event to see if there is a page to send that minute.
Sometimes there are multiple pages to be sent and I wanted to be sure that
all would be sent before the timer ticked again. Thanks for your answers!

You misunderstood me. :)

The answer to your original question is simply "no".

I also answered the questions "why won't it", and "how can I make it",
as I suspected that they might come.

I have answered enough questions to know that most people don't really
know what they are asking, or what they should be asking. ;)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
The timer will fire again after the 60 seconds are up, depending on
whether or not the message queue gets processed.

No, it won't.

I believe that you are thinking about a System.Timers.Timer or a
System.Threading.Timer, but this is a System.Windows.Forms.Timer. You
can tell because it's the only thing in the framework that has a Tick event.

This timer runs in the main thread, so when the main thread is busy and
is not processing messages, the timer will not fire.
If your timer tick
event handler is not re-entrant, then that could pose a problem.

No, not for a System.Windows.Forms.Timer. As it doesn't run in a
separate thread, it can't re-enter the event.
 
C

Curt Gough

Sorry, Göran. It is not the first time I have misunderstood. I do appreciate
your response, and the fact that you answered the unstated question.
 
C

Curt Gough

Actually, no. Typing and sending the message only took me 55 seconds. But,
thanks for the response.
;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top