XP System Clock Fast 
Author Message
 XP System Clock Fast

Hi Group,

My app polls a piece of hardware via a timer (1 second interval).  When the
app is running Windows XP's system clock runs fast by ~ 10 seconds every 20
minutes.  Has anyone seen this issue before or have an idea for solutions?
Thanks!!
--
Regards,
Chuck



Thu, 30 Jun 2011 20:05:42 GMT  
 XP System Clock Fast

Quote:

> Hi Group,

> My app polls a piece of hardware via a timer (1 second interval).
> When the app is running Windows XP's system clock runs fast by ~ 10
> seconds every 20 minutes.  Has anyone seen this issue before or
> have an idea for solutions? Thanks!!

The system clock doesn't run fast, but timer intervals are only
approximate.

The usual solution is to sample at a higher rate (whatever level of
precision is acceptable) and test the actual system timer in the timer
event. Air code:

Sub Timer1_Timer    ' set to 100ms

static LastTime as single

  if abs(LastTime - Timer) >= 1 then
     LastTime = Timer
     ' perform your actions
  end if

end sub

There are lots of possible variations, but they come down to the same
thing -- don't rely on the Timer.Interval, rely on the system clock.

--
    Jim Mack
    Twisted tees: we sew confusion
    http://www.cafepress.com/2050inc



Thu, 30 Jun 2011 20:34:28 GMT  
 XP System Clock Fast
My description of the problem may not have been clear.  My apps timer works
fine, however, the actual time on the computer clock (in the tray) runs
faster by ~10 seconds every 20 minutes when my app is running.  When my app
is closed the clock returns to normal.

--

Regards,
Chuck


Quote:

>> Hi Group,

>> My app polls a piece of hardware via a timer (1 second interval).
>> When the app is running Windows XP's system clock runs fast by ~ 10
>> seconds every 20 minutes.  Has anyone seen this issue before or
>> have an idea for solutions? Thanks!!

> The system clock doesn't run fast, but timer intervals are only
> approximate.

> The usual solution is to sample at a higher rate (whatever level of
> precision is acceptable) and test the actual system timer in the timer
> event. Air code:

> Sub Timer1_Timer    ' set to 100ms

> static LastTime as single

>  if abs(LastTime - Timer) >= 1 then
>     LastTime = Timer
>     ' perform your actions
>  end if

> end sub

> There are lots of possible variations, but they come down to the same
> thing -- don't rely on the Timer.Interval, rely on the system clock.

> --
>    Jim Mack
>    Twisted tees: we sew confusion
>    http://www.cafepress.com/2050inc



Thu, 30 Jun 2011 20:47:52 GMT  
 XP System Clock Fast


Quote:
> My description of the problem may not have been clear.  My apps timer works
> fine, however, the actual time on the computer clock (in the tray) runs
> faster by ~10 seconds every 20 minutes when my app is running.  When my app
> is closed the clock returns to normal.

Repeatedly reading a value will not alter its contents.  In order for the
clock to be affected by your program, you must be altering it some way.
How do you think that may be happening?

LFS



Thu, 30 Jun 2011 22:10:27 GMT  
 XP System Clock Fast


Quote:
> My description of the problem may not have been clear.  My apps timer
works
> fine, however, the actual time on the computer clock (in the tray) runs
> faster by ~10 seconds every 20 minutes when my app is running.  When my
app
> is closed the clock returns to normal.

I find that extraordinary. Not doubting you outright , but difficult to
accept at face value.

"Extraordinary claims require extraordinary evidence"  [Carl Sagan]

Can you produce an code example that demonstrates that phenomenon?

-ralph



Thu, 30 Jun 2011 22:02:17 GMT  
 XP System Clock Fast

Quote:

> My description of the problem may not have been clear.  My apps
> timer works fine, however, the actual time on the computer clock
> (in the tray) runs faster by ~10 seconds every 20 minutes when my
> app is running.  When my app is closed the clock returns to normal.

I've never seen or even heard of such a thing, which leads me to doubt
that it's systemic.

Do you do anything to alter system timing -- call timeSetInterval, use
Timer on the left side of an equation, etc?

What's the name of your VB Timer object? If it were named simply
Timer, you could cause confusion with the VB Timer variable.

--
   Jim Mack
   Twisted tees: we sew confusion
   http://www.cafepress.com/2050inc

Quote:

> "Jim Mack" wrote ...

>>> Hi Group,

>>> My app polls a piece of hardware via a timer (1 second interval).
>>> When the app is running Windows XP's system clock runs fast by ~
>>> 10 seconds every 20 minutes.  Has anyone seen this issue before or
>>> have an idea for solutions? Thanks!!

>> The system clock doesn't run fast, but timer intervals are only
>> approximate.

>> The usual solution is to sample at a higher rate (whatever level of
>> precision is acceptable) and test the actual system timer in the
>> timer event. Air code:

>> Sub Timer1_Timer    ' set to 100ms

>> static LastTime as single

>>  if abs(LastTime - Timer) >= 1 then
>>     LastTime = Timer
>>     ' perform your actions
>>  end if

>> end sub

>> There are lots of possible variations, but they come down to the
>> same thing -- don't rely on the Timer.Interval, rely on the system
>> clock.

>> --
>>    Jim Mack
>>    Twisted tees: we sew confusion
>>    http://www.cafepress.com/2050inc



Thu, 30 Jun 2011 22:29:42 GMT  
 XP System Clock Fast

Quote:
> I've never seen or even heard of such a thing, which leads me to doubt
> that it's systemic.

> Do you do anything to alter system timing -- call timeSetInterval, use
> Timer on the left side of an equation, etc?

> What's the name of your VB Timer object? If it were named simply
> Timer, you could cause confusion with the VB Timer variable.

> --
>   Jim Mack
>   Twisted tees: we sew confusion
>   http://www.cafepress.com/2050inc

The timer is named 'Timer1'.  I do not do anything intentionally to alter
the system clock.

--

Regards,
Chuck



Thu, 30 Jun 2011 22:38:22 GMT  
 XP System Clock Fast

Quote:
> Hi Group,

> My app polls a piece of hardware via a timer (1 second interval).  When
> the
> app is running Windows XP's system clock runs fast by ~ 10 seconds every
> 20
> minutes.  Has anyone seen this issue before or have an idea for solutions?
> Thanks!!

What type of hardware are you talking to?

How is it connected to the computer? Serial/Parallel/USB/PCI, etc.

Does the hardware have software or driver installed on the computer?

Some hardware use drivers that integrate with the timer used by Windows to
behave like a realtime clock. You maybe talking to the hardware incorrectly,
or there are bugs in the driver.

Also, VB6 has Date/Time statements which let you set the date and time, but
in this case Date/Time appear in the left side of =.

Finally, check this sample in how to synch the time back using a Time
server:

http://vbnet.mvps.org/code/network/winsocksynctime.htm



Thu, 30 Jun 2011 23:42:20 GMT  
 XP System Clock Fast

Quote:

>> I've never seen or even heard of such a thing, which leads me to
>> doubt that it's systemic.

>> Do you do anything to alter system timing -- call timeSetInterval,
>> use Timer on the left side of an equation, etc?

>> What's the name of your VB Timer object? If it were named simply
>> Timer, you could cause confusion with the VB Timer variable.

> The timer is named 'Timer1'.  I do not do anything intentionally to
> alter the system clock.

Since it doesn't happen except when your app is running, that means
your app is doing something to cause it, or whatever HW you're running
has a driver that causes it.

We don't have your source, so it's not possible to narrow it down any
further. But you can take some comfort in knowing that it _is_ your
app's doing, not some random or systemic problem. No one else
experiences this.

In your shoes I'd tear the app down to the smallest and simplest code
that exhibits the problem. Often that makes it obvious. Otherwise,
maybe you'll have something you can post here.

--

  Jim Mack
  Twisted tees: we sew confusion
  http://www.cafepress.com/2050inc



Fri, 01 Jul 2011 02:10:32 GMT  
 XP System Clock Fast

Quote:

> The timer is named 'Timer1'.  I do not do anything intentionally to alter
> the system clock.

Thing is, Timer events have a low priority, so there's absolutely no
guarantee that your 1 second timer is actually firing at exactly 1 second
intervals.

If you're actually counting timer ticks, you'll really be "in trouble" if a
screen saver starts, an AV app tries to get updated virus sigs, Windows
Update starts doing it's thing, etc, etc, etc.. The code below should be
reliable in all of those cases. The faster the interval, the more reliable
it would be. If coded correctly, there'd be very little overhead in the
Timer event handler that would need to run on every tick.... so, setting the
interval to a higher rate should cause no problems at all.
'--------------
Option Explicit
'Needs a command button (Command1), a Textbox (Text1) and a timer (Timer1)

Private mdtStartTime As Date
Private mbTimerEnabled As Boolean
Private mlNumberOfTicks As Long

Private Sub Command1_Click()
   mlNumberOfTicks = Val(Text1.Text)
   TimerEnabled = Not TimerEnabled
End Sub

Private Sub Form_Load()
   TimerEnabled = False
   Timer1.Interval = 100
   Text1.Text = "5"
End Sub

Private Property Get TimerEnabled() As Boolean
   TimerEnabled = mbTimerEnabled
End Property

Private Property Let TimerEnabled(ByVal State As Boolean)
   If State Then 'set caption = opposite state
      Command1.Caption = "Disable Timer"
      mdtStartTime = Now
   Else
      Command1.Caption = "Enable Timer"
   End If
   Timer1.Enabled = State
   mbTimerEnabled = State
End Property

Private Sub Timer1_Timer()
   Static lLastTick As Long
   Dim lCurrentTick As Long
   'a hires timer API call would be better (less overhead), but, since
you're measuring "seconds"
   'this should be fine for the experiment
   lCurrentTick = DateDiff("s", mdtStartTime, Now)
   If lLastTick <> lCurrentTick Then 'skip if less than 1 second
      lLastTick = lCurrentTick
      'This is where you'd do your polling
      Me.Caption = lCurrentTick

      'If shutting down after X seconds, this should work
      If lCurrentTick >= mlNumberOfTicks Then
         TimerEnabled = False
         Me.Caption = "Done"
      End If
   End If
End Sub
'--------------



Fri, 01 Jul 2011 03:25:58 GMT  
 XP System Clock Fast
This code when called from a timer will speed up the system clock?????  For
example, an app with just a timer with a 1 second interval repeatedly
calling 'WaitForMilliseconds' will cause the system clock to speed up.

*****************************************

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const MAX_HIRES_MILLISECONDS As Long = 500
Private Declare Sub SleepEx Lib "kernel32" (ByVal dwMilliseconds As Long, _
                            ByVal bAlertable As Boolean)

Public Sub WaitForMilliseconds(ByVal Milliseconds As Long)

Private Const TIME_ONESHOT As Long = &H0
Private Const TIME_PERIODIC As Long = &H1
Private Const TIME_CALLBACK_FUNCTION As Long = &H0
Private Const TIME_CALLBACK_EVENT_SET As Long = &H10
Private Const TIME_CALLBACK_EVENT_PULSE As Long = &H20

Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" _
                            (ByVal lpEventAttributes As Long, ByVal
bManualReset As Long, _
                             ByVal bInitialState As Long, ByVal lpName As
Any) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
                            (ByVal hHandle As Long, ByVal dwMilliseconds As
Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
                            (ByVal hObject As Long) As Long
Private Declare Function TimeSetEvent Lib "winmm" Alias "timeSetEvent" _
                            (ByVal uDelay As Long, ByVal uResolution As
Long, _
                             ByVal fptc As Long, ByVal dwUser As Long, _
                             ByVal fuEvent As Long) As Long
Private Declare Function TimeKillEvent Lib "winmm" Alias "timeKillEvent" _
                            (ByVal uTimerID As Long) As Long

Private m_hMultiTimerEvent As Long

    Dim ts As Long
    Dim delay As Long
    Dim uEventID As Long

    If (Milliseconds = 0) Or (Milliseconds > MAX_HIRES_MILLISECONDS) Then
        ts = GetTickCount()
        While (GetTickCount() - ts) < Milliseconds
            Call Yield
        Wend
    Else
        '
        ' "Short" wait, use the multimedia timer and put the
        ' app to sleep waiting on the completion event.
        '
        uEventID = TimeSetEvent(Milliseconds, 0&, m_hMultiTimerEvent, 0&, _
                                (TIME_ONESHOT + TIME_CALLBACK_EVENT_SET))
        If uEventID = 0 Then Err.Raise SCODE_TIMER_FAIL, ERR_SOURCE_UTIL,
MSG_TIMER_FAIL
        Call WaitForSingleObject(m_hMultiTimerEvent, MAX_HIRES_MILLISECONDS)
        Call TimeKillEvent(uEventID)
    End If

End Sub

Public Sub Yield()

    SleepEx 10, True
    DoEvents

End Sub

*******************************************
--

Regards,
Chuck


Quote:


>> The timer is named 'Timer1'.  I do not do anything intentionally to alter
>> the system clock.

> Thing is, Timer events have a low priority, so there's absolutely no
> guarantee that your 1 second timer is actually firing at exactly 1 second
> intervals.

> If you're actually counting timer ticks, you'll really be "in trouble" if
> a screen saver starts, an AV app tries to get updated virus sigs, Windows
> Update starts doing it's thing, etc, etc, etc.. The code below should be
> reliable in all of those cases. The faster the interval, the more reliable
> it would be. If coded correctly, there'd be very little overhead in the
> Timer event handler that would need to run on every tick.... so, setting
> the interval to a higher rate should cause no problems at all.
> '--------------
> Option Explicit
> 'Needs a command button (Command1), a Textbox (Text1) and a timer (Timer1)

> Private mdtStartTime As Date
> Private mbTimerEnabled As Boolean
> Private mlNumberOfTicks As Long

> Private Sub Command1_Click()
>   mlNumberOfTicks = Val(Text1.Text)
>   TimerEnabled = Not TimerEnabled
> End Sub

> Private Sub Form_Load()
>   TimerEnabled = False
>   Timer1.Interval = 100
>   Text1.Text = "5"
> End Sub

> Private Property Get TimerEnabled() As Boolean
>   TimerEnabled = mbTimerEnabled
> End Property

> Private Property Let TimerEnabled(ByVal State As Boolean)
>   If State Then 'set caption = opposite state
>      Command1.Caption = "Disable Timer"
>      mdtStartTime = Now
>   Else
>      Command1.Caption = "Enable Timer"
>   End If
>   Timer1.Enabled = State
>   mbTimerEnabled = State
> End Property

> Private Sub Timer1_Timer()
>   Static lLastTick As Long
>   Dim lCurrentTick As Long
>   'a hires timer API call would be better (less overhead), but, since
> you're measuring "seconds"
>   'this should be fine for the experiment
>   lCurrentTick = DateDiff("s", mdtStartTime, Now)
>   If lLastTick <> lCurrentTick Then 'skip if less than 1 second
>      lLastTick = lCurrentTick
>      'This is where you'd do your polling
>      Me.Caption = lCurrentTick

>      'If shutting down after X seconds, this should work
>      If lCurrentTick >= mlNumberOfTicks Then
>         TimerEnabled = False
>         Me.Caption = "Done"
>      End If
>   End If
> End Sub
> '--------------



Fri, 01 Jul 2011 20:47:36 GMT  
 XP System Clock Fast
No... what I was getting at is... timer events are postponed and sometimes
skipped (if they overlap) on a whim, by the OS, so the interval is really
the minimum time between ticks. System overhead, code in the timer event,
etc, etc, can cause timers to be inaccurate.

Plus, if you're using the Sleep(ex) API, it can delay the timer, too. It
puts the entire thread to sleep, including the timers running on that
thread. Windows is, in no way, a "real time" operating system... a fact I've
been hammered by and have had to work around, many times in the past.

If it seems like the system clock is speeding up, it makes more sense to
assume the code is not tracking the time correctly... a missing tick, etc,
can cause your code based timer, or time measurment, to appear slow... not
the system clock to appear fast. If your system clock is actually running
fast, there's not much anyone can do.. it's a hardware problem.


Quote:
> This code when called from a timer will speed up the system clock?????
> For example, an app with just a timer with a 1 second interval repeatedly
> calling 'WaitForMilliseconds' will cause the system clock to speed up.

> *****************************************

> Private Declare Function GetTickCount Lib "kernel32" () As Long
> Private Const MAX_HIRES_MILLISECONDS As Long = 500
> Private Declare Sub SleepEx Lib "kernel32" (ByVal dwMilliseconds As Long,
> _
>                            ByVal bAlertable As Boolean)

> Public Sub WaitForMilliseconds(ByVal Milliseconds As Long)

> Private Const TIME_ONESHOT As Long = &H0
> Private Const TIME_PERIODIC As Long = &H1
> Private Const TIME_CALLBACK_FUNCTION As Long = &H0
> Private Const TIME_CALLBACK_EVENT_SET As Long = &H10
> Private Const TIME_CALLBACK_EVENT_PULSE As Long = &H20

> Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" _
>                            (ByVal lpEventAttributes As Long, ByVal
> bManualReset As Long, _
>                             ByVal bInitialState As Long, ByVal lpName As
> Any) As Long
> Private Declare Function WaitForSingleObject Lib "kernel32" _
>                            (ByVal hHandle As Long, ByVal dwMilliseconds As
> Long) As Long
> Private Declare Function CloseHandle Lib "kernel32" _
>                            (ByVal hObject As Long) As Long
> Private Declare Function TimeSetEvent Lib "winmm" Alias "timeSetEvent" _
>                            (ByVal uDelay As Long, ByVal uResolution As
> Long, _
>                             ByVal fptc As Long, ByVal dwUser As Long, _
>                             ByVal fuEvent As Long) As Long
> Private Declare Function TimeKillEvent Lib "winmm" Alias "timeKillEvent" _
>                            (ByVal uTimerID As Long) As Long

> Private m_hMultiTimerEvent As Long

>    Dim ts As Long
>    Dim delay As Long
>    Dim uEventID As Long

>    If (Milliseconds = 0) Or (Milliseconds > MAX_HIRES_MILLISECONDS) Then
>        ts = GetTickCount()
>        While (GetTickCount() - ts) < Milliseconds
>            Call Yield
>        Wend
>    Else
>        '
>        ' "Short" wait, use the multimedia timer and put the
>        ' app to sleep waiting on the completion event.
>        '
>        uEventID = TimeSetEvent(Milliseconds, 0&, m_hMultiTimerEvent, 0&, _
>                                (TIME_ONESHOT + TIME_CALLBACK_EVENT_SET))
>        If uEventID = 0 Then Err.Raise SCODE_TIMER_FAIL, ERR_SOURCE_UTIL,
> MSG_TIMER_FAIL
>        Call WaitForSingleObject(m_hMultiTimerEvent,
> MAX_HIRES_MILLISECONDS)
>        Call TimeKillEvent(uEventID)
>    End If

> End Sub

> Public Sub Yield()

>    SleepEx 10, True
>    DoEvents

> End Sub

> *******************************************
> --

> Regards,
> Chuck





>>> The timer is named 'Timer1'.  I do not do anything intentionally to
>>> alter the system clock.

>> Thing is, Timer events have a low priority, so there's absolutely no
>> guarantee that your 1 second timer is actually firing at exactly 1 second
>> intervals.

>> If you're actually counting timer ticks, you'll really be "in trouble" if
>> a screen saver starts, an AV app tries to get updated virus sigs, Windows
>> Update starts doing it's thing, etc, etc, etc.. The code below should be
>> reliable in all of those cases. The faster the interval, the more
>> reliable it would be. If coded correctly, there'd be very little overhead
>> in the Timer event handler that would need to run on every tick.... so,
>> setting the interval to a higher rate should cause no problems at all.
>> '--------------
>> Option Explicit
>> 'Needs a command button (Command1), a Textbox (Text1) and a timer
>> (Timer1)

>> Private mdtStartTime As Date
>> Private mbTimerEnabled As Boolean
>> Private mlNumberOfTicks As Long

>> Private Sub Command1_Click()
>>   mlNumberOfTicks = Val(Text1.Text)
>>   TimerEnabled = Not TimerEnabled
>> End Sub

>> Private Sub Form_Load()
>>   TimerEnabled = False
>>   Timer1.Interval = 100
>>   Text1.Text = "5"
>> End Sub

>> Private Property Get TimerEnabled() As Boolean
>>   TimerEnabled = mbTimerEnabled
>> End Property

>> Private Property Let TimerEnabled(ByVal State As Boolean)
>>   If State Then 'set caption = opposite state
>>      Command1.Caption = "Disable Timer"
>>      mdtStartTime = Now
>>   Else
>>      Command1.Caption = "Enable Timer"
>>   End If
>>   Timer1.Enabled = State
>>   mbTimerEnabled = State
>> End Property

>> Private Sub Timer1_Timer()
>>   Static lLastTick As Long
>>   Dim lCurrentTick As Long
>>   'a hires timer API call would be better (less overhead), but, since
>> you're measuring "seconds"
>>   'this should be fine for the experiment
>>   lCurrentTick = DateDiff("s", mdtStartTime, Now)
>>   If lLastTick <> lCurrentTick Then 'skip if less than 1 second
>>      lLastTick = lCurrentTick
>>      'This is where you'd do your polling
>>      Me.Caption = lCurrentTick

>>      'If shutting down after X seconds, this should work
>>      If lCurrentTick >= mlNumberOfTicks Then
>>         TimerEnabled = False
>>         Me.Caption = "Done"
>>      End If
>>   End If
>> End Sub
>> '--------------



Sun, 03 Jul 2011 11:58:45 GMT  
 XP System Clock Fast
Try using this free software, which syncs to atomic clocks, but keeps a log,
so you can see what effects your software has:

http://www.gregorybraun.com/WebTime.html

I am not sure why your code would affect the system clock. I once had a
routine that calls an API function that blocks for about 50 seconds. When I
compare the return value of GetTickCount before and after the blocking call,
I get the same value, when I do a DoEvents, GetTickCount returns the correct
time. It's like each thread has it's own copy of TickCount. Try calling the
non-Ex versions of the functions to see if that makes any difference.



Sun, 03 Jul 2011 12:26:21 GMT  
 XP System Clock Fast
Yeah, I verified the speed up by syncing my system clock with the time
server, then running my app for 20 minutes.  A comparison to the NIST clock
showed the system time running fast.  I then  did the same test without my
app running and the system clock kept it's timing synced with the NIST
clock.  I will just have to rewrite the code knowing now that this oddity
exists......

Thanks everyone.

Regards,
Chuck

Quote:
> Try using this free software, which syncs to atomic clocks, but keeps a
> log, so you can see what effects your software has:

> http://www.gregorybraun.com/WebTime.html

> I am not sure why your code would affect the system clock. I once had a
> routine that calls an API function that blocks for about 50 seconds. When
> I compare the return value of GetTickCount before and after the blocking
> call, I get the same value, when I do a DoEvents, GetTickCount returns the
> correct time. It's like each thread has it's own copy of TickCount. Try
> calling the non-Ex versions of the functions to see if that makes any
> difference.



Sun, 03 Jul 2011 19:14:46 GMT  
 XP System Clock Fast

Have a look here...
http://support.microsoft.com/kb/821893

Bantu Bob

*** Sent via Developersdex http://www.developersdex.com ***



Sun, 11 Sep 2011 21:04:58 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Binding Action Queries to System Clock.

2. Compare Scheduled Event Time to System Clock

3. Starting a VB Program with System Clock

4. Why system clock become late?

5. using system clock to iniate a fuction

6. System Clock

7. Synchenize System clock

8. Indexing the system clock

9. How do I access the system clock?

10. System Clock Updates in NT

11. System Tray Clock

12. Altering the System Clock:WARNING

 

 
Powered by phpBB® Forum Software