Timing issues in Windows code
Hi,
I have written some code that mimics the Grenwich time signal beeps. The programme spends most of the time sleeping. The programme exits sleep just before 5 seconds to the hour, waits for 55 seconds then beeps, then a short sleep waits for 4 seconds to the hour then another beep and so on.
The code fragment is:
Code:
/* Sleep until just before 55 seconds of minute */
Sleep(millisecondsuntiltoh - 5050);
/* Wait until seconds reach 55 */
while(seconds!=55)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
/* Sleep until just before 56 seconds of minute */
Sleep(shortsleep);
/* Wait until seconds reach 56 */
while(seconds!=56)
{
GetSystemTime(&str_t);
seconds = str_t.wSecond;
}
Beep(1000, shortbeep);
My problem is that the first beep is noticeably late. Subsequent beeps appear to happen on time. This happens even though the programme has finished the long sleep in time. I know that Windows is not a real-time operating system but what am I doing wrong?
Any thoughts or ideas appreciated.
Thank you.