Pausing execution for a set number of microseconds

Does anybody please know of any functions, part of a complete library or otherwise, which will pause the program execution for a set number of microseconds? I have tried using the win32 sleep function, but it only works with whole seconds and is therefore useless (in case anybody's interested, I need this because I am modifying a windows game I programmed a while back so that it's playable on faster computers. At present using my high spec system it is far to fast to be playable, but at the same time it needs to still be playable on slower computers. In future I will ensure that all games I write are future aware).
____________________________________________
Freeware, Stories, Reviews and misellaneous claptrap

www.amattgreen.com

Comments

  • : Does anybody please know of any functions, part of a complete library or otherwise, which will pause the program execution for a set number of microseconds? I have tried using the win32 sleep function, but it only works with whole seconds

    [blue]you must have not read the correct instructions. Win32 API function is Sleep(int milliseconds) (UPPER-CASE 'S'), not sleep(int seconds) (lower-case 's') -- which is a unix function.[/blue]



  • I've found that depending on what compiler and whether I'm building a win32 program or console program the sleep() will compile differently. With Dev-C++ a console application of sleep() will be in milliseconds, but with my Borland compiler sleep() will be in seconds. My Borland package offers delay() in the dos.h header which operates in milliseconds. But, delay() isn't referenced anywhere else that I can find. I also researched various parts of time.h, like difftime() but everything there seems to be based only as low as seconds. You may need to try a different compiler to get the sleep() version in milliseconds if you're compiling a console program. A win32 program does seem to always use milliseconds as the unit for sleep() in the compilers I've played with. Sorry I wasn't more helpful.

    Take Care,
    Ed

    : Does anybody please know of any functions, part of a complete library or otherwise, which will pause the program execution for a set number of microseconds? I have tried using the win32 sleep function, but it only works with whole seconds and is therefore useless (in case anybody's interested, I need this because I am modifying a windows game I programmed a while back so that it's playable on faster computers. At present using my high spec system it is far to fast to be playable, but at the same time it needs to still be playable on slower computers. In future I will ensure that all games I write are future aware).
    : ____________________________________________
    : Freeware, Stories, Reviews and misellaneous claptrap
    :
    : www.amattgreen.com
    :

  • [b][red]This message was edited by JBaker at 2004-10-11 11:42:50[/red][/b][hr]
    [b][red]This message was edited by JBaker at 2004-10-11 11:34:15[/red][/b][hr]
    : Does anybody please know of any functions, part of a complete library or otherwise, which will pause the program execution for a set number of microseconds? I have tried using the win32 sleep function, but it only works with whole seconds and is therefore useless (in case anybody's interested, I need this because I am modifying a windows game I programmed a while back so that it's playable on faster computers. At present using my high spec system it is far to fast to be playable, but at the same time it needs to still be playable on slower computers. In future I will ensure that all games I write are future aware).
    : ____________________________________________
    : Freeware, Stories, Reviews and misellaneous claptrap
    :
    : www.amattgreen.com
    :
    [red]The only way I know how to get reliable microsecond resolution is by using assembly to do a push and pop, know how many CPU cycles that takes and then use the CPU frequency as a divider.

    Disregard the rest or this post, none of which I refer to below has microsecond resolution. Sorry, my bad.[/red]

    [green]You may also want to look at the SetTimer function. I know M$ and Borland support this Win32 function. It has a resolution of 1mS.

    Borland has the gettime function and M$ supports the clock function, both of which has mS precision.

    Hope this helps[/green]
    [blue]JBaker[/blue]
  • I have used the following code across several Intel platforms and it seems to be very accurate, even though the ancient DOS programmers reference that I use (DOS Programmers Reference 2nd Edition) claims it to be accurate only to within 976 microseconds.

    I use the function below to time step pulses in an open loop stepper system, and if it were truly that inaccurate I do not believe my code would work as accurately as it does.

    A question for the code gurus out there: The reference I have in this regard is written for the 8086, and up to DOS ver. 4.0. Is it possible that overall improvements in system speed have made this call more accurate?:

    At any rate, here it is:


    [code]

    // (1,000,000 Microseconds = 1 second)

    void MicroDelay (long MicroSecs)
    {
    _asm {
    mov cx, word ptr [MicroSecs+2]
    mov dx, word ptr [MicroSecs]
    mov ah, 0x86
    int 0x15
    }
    }

    // For delay in Milliseconds (1000 Milliseconds = 1 second)

    void MilliDelay (long MilliSecs)
    {
    MicroDelay (MilliSecs * 1000);
    }

    [/code]



    : Does anybody please know of any functions, part of a complete library or otherwise, which will pause the program execution for a set number of microseconds? I have tried using the win32 sleep function, but it only works with whole seconds and is therefore useless (in case anybody's interested, I need this because I am modifying a windows game I programmed a while back so that it's playable on faster computers. At present using my high spec system it is far to fast to be playable, but at the same time it needs to still be playable on slower computers. In future I will ensure that all games I write are future aware).
    : ____________________________________________
    : Freeware, Stories, Reviews and misellaneous claptrap
    :
    : www.amattgreen.com
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories