Thread: Delay using timeGetTime is too short

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Delay using timeGetTime is too short

    Ok I understand that by trying to create a delay using timeGetTime I'll get an approximate delay, not an exact one. But if anything it should be AT LEAST the amount of time I tell it to delay. My problem is:

    I have one
    printf ("time is: %i", timeGetTime());
    statement before and one after executing the following code. The difference between the first and the second outputs is always much less than 1 sec (usually between 500 and 600 ms)...

    Code:
    int Delay1sec(void)
    {
    	static unsigned long ultime = timeGetTime()+1000;
    	while (timeGetTime()<ultime)
    	{ Sleep(5);	}
    	ultime = timeGetTime()+1000;
    	return 0;
    }
    What am I doing wrong?
    Last edited by doia; 07-16-2010 at 04:15 PM. Reason: typo

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not just Sleep(1000)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    From MSDN

    Windows Multimedia
    Previous Next

    timeGetTime
    The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.

    DWORD timeGetTime(VOID);
    Parameters

    This function does not take parameters.

    Return Values

    Returns the system time, in milliseconds.

    Remarks

    The only difference between this function and the timeGetSystemTime function is that timeGetSystemTime uses the MMTIME structure to return the system time. The timeGetTime function has less overhead than timeGetSystemTime.

    Note that the value returned by the timeGetTime function is a DWORD value. The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days. This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations.

    Windows NT/2000: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum difference between successive values returned by timeGetTime can be as large as the minimum period value set using timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution,

    Windows 95: The default precision of the timeGetTime function is 1 millisecond. In other words, the timeGetTime function can return successive values that differ by just 1 millisecond. This is true no matter what calls have been made to the timeBeginPeriod and timeEndPeriod functions.

    Requirements

    Windows NT/2000/XP: Included in Windows NT 3.1 and later.
    Windows 95/98/Me: Included in Windows 95 and later.
    Header: Declared in Mmsystem.h; include Windows.h.
    Library: Use Winmm.lib.

    See Also

    Multimedia Timers, Multimedia Timer Functions, timeGetSystemTime, MMTIME, timeGetTime, timeBeginPeriod, timeEndPeriod, QueryPerformanceCounter, QueryPerformanceFrequency

    Previous Next

    --------------------------------------------------------------------------------

    © 2005 Microsoft Corporation. All rights reserved.
    Since the default res is >= 5 ms you may not get accurate timing unless you call timeBeginPeriod() and timeEndPeriod() to set the resolution. For high performance timing you should look into QueryPerformanceFrequency() and QueryPerformanceCounter(). Note that on AMD dual core machines you may need to also call SetProcessAffinityMask() since the BIOS's on those machines do not accurately handle the performance counters from the 2 cores.
    Last edited by VirtualAce; 07-17-2010 at 10:14 PM.

  4. #4
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    NVM I found out what the problem was. My application(it's an infinite loop) was taking longer to process than 1 sec sometimes an therefore the delay wouldn't work in this case because I declared it as a static variable.
    Sleep is usually not a good choice in time-critical applications because it has poor resolution. Also I needed a function that would take into account the amount of time elapsed since the delay's last execution.
    Thanks Elysia and bubba.
    Last edited by doia; 07-19-2010 at 08:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM