Skip to content

Timer and DateTime — clock goes wrong

niketas

Asked Apr 7, 2008, 2:11 PM EDT1 answer
Share
LinkedInX
Greetings.
First of all, I'm very sorry for my English.
Trying to create my own virtual clock with a function of time-accelerating. I want clock hands to rotate smoothly, so to accelerate clock speed I managed to go on with timer this way: I set up a timer with an interval of 1 millisecond, and when timer ticks, this procedure helds:

private void timer1_Tick(object sender, System.EventArgs e)
        {
                s  = (s + (double)hScrollBar1.Value / 100) % 60;
                //hScrollBar1.Value is a coefficient of accelerating
        }

So each 1 ms the s variable, responsible for seconds hand, increases by 0.01.

I have 2 questions:
1. When things go on like this, why there are 100 ms in a second instead of usual 1000?
2. Important: why clock is late approx. by 1.6 times to a usual DateTime clock when I use timer and timer_Tick inreasing of s (h, m, ms) variable? E.g., in “my clock's world” passes only 40 seconds, while in real world 64 has gone.

Appreciate your input very much! Can provide source project at any time, please, explain and/or help.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    This is an answer to the second part of your question:Why is it late?

    The windows timer event is queued up with other windows events on the same UI thread and has about a 55 ms resolution. It is not an accurate way to handle time.  You are better off getting the exact time from DateTime.Now every time you enter the timer event and using this time

    OR use the System.Timers namespace which is more accurate

    0