Timer

T

Tony Johansson

Hi!

Assume I start a Timer that will call the event handler for this Timer every
second.
Assume also that the event handler method processtime is longer then 1
second what will happen then

//Tony
 
R

Rick Lones

Tony said:
Hi!

Assume I start a Timer that will call the event handler for this Timer every
second.
Assume also that the event handler method processtime is longer then 1
second what will happen then

Something bad, depending on how the handler is written and especially on whether
it yields control. You should try it for yourself if you really need to know.
But why would you care unless you were actually planning to do something this
fundamentally silly in an actual application?

-rick-
 
A

Arne Vajhøj

Hi!

Assume I start a Timer that will call the event handler for this Timer every
second.
Assume also that the event handler method processtime is longer then 1
second what will happen then

You are wondering what the following program will
output ?

using System;
using System.Timers;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
Timer t = new Timer(1000.0);
t.Elapsed += delegate(object sender, ElapsedEventArgs e) {
DateTime dt = DateTime.Now;
System.Threading.Thread.Sleep(2000);
Console.WriteLine(dt + " - " + DateTime.Now);
};
t.Start();
System.Threading.Thread.Sleep(10000);
t.Stop();
Console.ReadKey();
}
}
}

Arne
 
R

Rick Lones

Steve said:
To be fair to the OP, I doubt he's actually planning it. He just wants
to know what would happen should such a situation arise.

You're right that there is nothing wrong with just wondering.
Won't it just queue the relevant windows messages (timer events) against
the application? I'm not sure what happens if the message queue gets
longer and longer - presumably the OS will handle it by reporting the
application as "Stopped responding". Is that correct?

SteveT

There are just a lot of possible scenarios here ranging from benign to fatal. I
would hope the OP try some of them, with variations. You learn so much from
untangling the resulting mess and understanding what happened for yourself.
"When in doubt, ask the machine."

Consider the case where you use up an interval's worth of time (without
disabling the timer during processing) and then yield to the OS via DoEvents().
Your pending next timer event will be fired before your current one has
finished. Now your handler has just become re-entrant whether or not it was
written to be plus you are heading for an eventual stack overflow.

Or on the other hand, suppose you disable the timer on entry to the handler and
don't re-enable it until exit. There may be no noticeable bad effect at all,
but your app will be missing some of those interval boundaries it supposedly
cares about. Q: How bad is that? A: "It depends."

-rick-
 

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

Similar Threads

timer... 3
about Timer 3
My timer doesn't work 1
System.Windows.Forms.Timer 5
about delegete 3
Passing EventHandler 1
Timers and WebServices 2
Events and Dispose 1

Top