Thread: Timing issues in Windows code

  1. #16
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    Thank you rcgldr. I'm currently using Windows 10. My old laptop has a little internal speaker which is where the beep sound emanates from.

  2. #17
    Registered User
    Join Date
    Apr 2013
    Posts
    1,582
    I don't have Visual Studio installed on Win 10 yet (multi-boot system). The code below is working with Windows XP / Visual Studio 2005 and Windows 7 / Visual Studio 2015. I used the #if to switch to beeping every minute. Sleep uses DWORD (unsigned int) for input parameter, so I changed a few things to DWORD or unsigned. I changed short_sleep to 625 ms to make it a multiple of 15.625 ms (1/64hz).

    Code:
    #include <windows.h>
     
    #define MAIN_SLEEP_DIF  6000
    #define SHORT_SLEEP      625
    #define SHORT_BEEP       100
    #define LONG_BEEP        500
    #define FREQ            1000
     
    int main() {
        SYSTEMTIME tim;
        DWORD ms_until_toh;
        DWORD secs;
    
        while (TRUE) {
            GetSystemTime(&tim);
    #if 0
            ms_until_toh = 3600000u
                         - tim.wMinute * 60000u
                         - tim.wSecond * 1000u
                         - tim.wMilliseconds;
    #else
            ms_until_toh = 60000u
                - tim.wSecond * 1000u
                - tim.wMilliseconds;
    #endif
    
            if (ms_until_toh > MAIN_SLEEP_DIF)
                Sleep(ms_until_toh - MAIN_SLEEP_DIF);
    
            for (secs = 55; secs < 60; secs++) {
                do
                    GetSystemTime(&tim);
                while (tim.wSecond < secs);
                Beep(FREQ, SHORT_BEEP);
                Sleep(SHORT_SLEEP);
            }
     
            do
                GetSystemTime(&tim);
            while (tim.wSecond != 0);
            Beep(FREQ, LONG_BEEP);
        }
         
        return 0;
    }
    Last edited by rcgldr; 2 Weeks Ago at 01:47 PM.

  3. #18
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    Thank you. I think it worked ok for me Beeping every minute. Can you try hourly and see if it still works ok?

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    1,582
    Quote Originally Posted by Fat Agnus View Post
    Thank you. I think it worked ok for me Beeping every minute. Can you try hourly and see if it still works ok?
    Just change the "#if 0" to "#if 1". It worked when I manually set the time to just before an hour.

  5. #20
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    I believe it's the long sleep causing an issue with processor time allocation.

  6. #21
    Registered User
    Join Date
    Apr 2013
    Posts
    1,582
    Quote Originally Posted by Fat Agnus View Post
    I believe it's the long sleep causing an issue with processor time allocation.
    I haven't tried doing a long sleep. I have now tried the 1 minute version on Windows 10 with VS 2015. I also changed MAIN_SLEEP_DIF from 6000 to 5375 and it still works. If the long sleep is an issue, you could change the code to sleep 1 minute at a time, as it would probably use less than 1ms cpu time per minute.

  7. #22
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    Quote Originally Posted by rcgldr View Post
    You didn't mention which version of Windows you're using. Beep() uses the speaker on Windows XP, doesn't work at all on Windows Vista, and uses the sound card on Windows 7 or later. Multiple beep calls in Windows XP work, but in Windows 7, there's a short bit of silence between beeps.
    My test/s seem to indicate that it is the Beep() function itself causing the delay. Can you think of any way around this using Windows 10. I have looked at making a call to the BIOS but it doesn't seem straight forward.

    EDIT: Is there an alternative to the Beep() function?
    Last edited by Fat Agnus; 2 Weeks Ago at 07:02 PM.

  8. #23
    Registered User
    Join Date
    Apr 2013
    Posts
    1,582
    Quote Originally Posted by Fat Agnus View Post
    My test/s seem to indicate that it is the Beep() function itself causing the delay. Is there an alternative to the Beep() function?
    The delays you have for Beep() are well less than 1 second, so that shouldn't be an issue. I haven't confirmed if Windows 10 runs the ticker at 64 hz == 15.625 ms per tick, but assuming so, then a beep of 100 ms will become 109.375 ms and a beep of 500 ms or any multiple of 125 ms (8 hz) will be exact.

    An alternative to Beep() would be to play a .wav file like sequence through the sound card. There may be a way to get the PC speaker to output a sound.
    Last edited by rcgldr; 1 Week Ago at 02:35 PM.

  9. #24
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    Than you rcgldr. The delay seems to be the time to make the actual Beep() from making a call to the function. There is not always a noticeable delay but there is often on the first Beep(). I have been trying to use the SDL2 library in place of Beep(). This seems to work without delay although requires more code...

    I am using your version of my program in #17 with SDL2. I need to tidy my new SDL2 additions up but for now it seems to work and I can post the code soon. One thing we can't seem to get away from is that there be tens or even hundreds of milliseconds delay in executing code on a multitasking operating system. Playing a wav with sound effect would be a solution to avoid timing issues once the wav is playing and could be easily computed. My SDL2 version already plays a sinewave computed in memory (from an example found on the net).

  10. #25
    Registered User
    Join Date
    Apr 2013
    Posts
    1,582
    Quote Originally Posted by Fat Agnus View Post
    One thing we can't seem to get away from is that there be tens or even hundreds of milliseconds delay in executing code on a multitasking operating system.
    Try setting priority to "above normal" or "high". There shouldn't be any background apps or services running at the higher priorities, so a context switch to your program after a sleep should be fairly quick.

    SetPriorityClass function (Windows)

    GetCurrentProcess function (Windows)

    Plus a few more of these Windows specific functions.
    Last edited by rcgldr; 1 Week Ago at 06:12 PM.

  11. #26
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    Than you rcgld, I will try that and see if it evens out any (now) small timing variations.

    At the moment the program opens a console window. I'm using the MinGW complier in the codeblocks IDE. I would like my Sleep(); to be interrupted by a close window or CTRL_CLOSE_EVENT or whatever it's called so I can clean up and exit. It does not appear clear to me if Sleep() is interrupted by such events, my test seem to show that a CTRL_C event for example does not interrupt Sleep). I have been looking and experimenting with handlers for console events. Do you have any knowledge in this area? Thank you.

  12. #27
    Registered User
    Join Date
    Apr 2013
    Posts
    1,582
    Quote Originally Posted by Fat Agnus View Post
    I will try that and see if it evens out any (now) small timing variations.
    Example code to set a Windows process to high priority.

    Code:
    /* ... */
        HANDLE hProcess;
    /* ... */
        hProcess = GetCurrentProcess();     /* set to high priority */
        if(!SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
            return 0;                       /* return if failed to set priority */
    or

    Code:
    /* ... */
        if(!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS))
            return 0;                       /* return if failed to set priority */

  13. #28
    Registered User
    Join Date
    Sep 2017
    Posts
    15
    Thank you rcgldr. I will give that a try. Now that I am no longer using Beep() I no longer have the delay/s in the sound. I don't believe the context switch to my task was really an issue as the sleep was exited in time just that (at least) the initial Beep() was late. Raising my task to higher priority may give me more accurate timing though I'm not asking or expecting other than a reasonable marking of the hour without really obvious timing variations in the Pips.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux and Windows write timing difference
    By chipbu in forum C Programming
    Replies: 2
    Last Post: 11-20-2012, 02:36 AM
  2. Simple game with timing issues atm
    By Swarvy in forum Game Programming
    Replies: 1
    Last Post: 08-06-2009, 09:52 PM
  3. Timing measurements on windows
    By shani in forum Windows Programming
    Replies: 16
    Last Post: 12-19-2007, 01:59 PM
  4. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 12:43 AM
  5. timing issues
    By jibblit in forum C Programming
    Replies: 6
    Last Post: 03-25-2002, 10:26 AM

Tags for this Thread