🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.misc
Article #2113

Re: Inaccuracy in windows timer?

#2113
From: Stephen Oakes
Date: Wed, 18 Nov 1992 05:05
38 lines
2615 bytes
In article <1992Nov18.113155.165165@dstos3.dsto.gov.au> jav@dstos3.dsto.gov.au writes:
>Windows timer inaccuracy!
>
>I have an application which needs to sample a value every 60 seconds.  I use
>the settimer function with an interval of 60000 to send a message to the
>appropriate window which invokes the relevant member function to do the
>sampling (I'm using Boralnd C++ 3.1 with OWL).  This all works fine except that
>the sampling time actually creeps by about 1 second every thirty minutes.  I
>store the time (obtained using standard C time.h functions from the PC clock)
>in a file with every sample and after thirty minutes or so, the sampling occurs
>one second later in the minute than originally.

Never ever *ever* *EVER* rely upon the Windows timer to give you accurate
timing.  There are several issues here.  One of them is that (according
to the docs) you are not even guaranteed that you will get every timer message,
because they are low priority messages, and if one stays in the message queue
long enough (until another one is placed there) Windows will remove the first
one.  Or you might just get a message a few seconds late if Windows is busy
with another app.

Also, although the docs say that the parameter passed to SetTimer
is in milliseconds, I wonder if they actually use 1/1024 instead?

To avoid the slippage you describe, I think there are three possibilities:
	1. Use SetTimer to send you a message far more frequently (say
	   every second) and then read the actual time to determine
	   whether you should do the sample.
	2. Similar to the above, except do it in a PeekMessage loop
	   instead of using a timer.  This may degrade Windows'
	   performance slightly.
	3. I think you can use the DDK to get more accurate timings,
	   but I don't know anything about this.  

--
| Stephen Oakes      CSIRO Division of Building, Construction and Engineering |
|                    Post Office Box 56, Highett, Victoria, Australia   3190  |
| Internet: steveo@mel.dbce.csiro.au  Tel: +61 3 252 6000 Fax: +61 3 252 6249 |
|_____________________________________________________________________________|
Technical Details
Message-ID: <576@highett.mel.dbce.csiro.au>
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!rpi!batcomputer!munnari.oz.au!mel.dit.csiro.au!highett!steveo
References: <1992Nov18.113155.165165@dstos3.dsto.gov.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!rpi!batcomputer!munnari.oz.au!mel.dit.csiro.au!highett!steveo
From: steveo@mel.dbce.csiro.au (Stephen Oakes)
Newsgroups: comp.os.ms-windows.programmer.misc
Subject: Re: Inaccuracy in windows timer?
Message-ID: <576@highett.mel.dbce.csiro.au>
Date: 18 Nov 92 05:05:47 GMT
References: <1992Nov18.113155.165165@dstos3.dsto.gov.au>
Organization: CSIRO, Div. Building Constr. and Eng'ing, Melb., Australia
Lines: 38