Microsecond delay

Here is a sample using system resources available to get a MicroSecond delay function.

As we know the system has WM_TIMER which gives about 50mS resolution, and Mutlimedia Timers which can do about 1mS, and if one require a simple delay of say 100microseconds with about 5% accuracy there is nothing available to do that.

Using the GetTickCount() it is possible to adjust a simple for() loop that contains _asm {NOP} and get a reasonable delay accuracy.

The attached code sample explains itself.

One improvement I would make next time I use this, is to save the initialization values to the registry, and avoid doing it every time the program starts.

The code has been tested on three systems ( dual pentuim Pro running at 233MHz ) ( Penitum P5 at 233MHz) and Toshiba Laptop running at 133MHz.

Project File 31K



Comments

  • Anyone can explain NdisMSleep(IN ULONG MicrosecondsToSleep)?

    Posted by Legacy on 02/22/2004 12:00am

    Originally posted by: Alex Yang

    Has anyone used
    VOID NdisMSleep(IN ULONG MicrosecondsToSleep) ?

    I wonder if it is usable on most computers and how to use it. It's from NT DDK I don't have but I can find its header file in \CYGWIN\USR\INCLUDE\W32API\DDK though.

    Reply
  • Another Delay function...

    Posted by Legacy on 05/22/2003 12:00am

    Originally posted by: Alex

    I think that a good Delay function should not lock the 
    
    application while is running, this because the user
    interface should be available for other things. For
    example, if you want to click a button while there's a
    Sleep function of 5 seconds running, the thread of the user
    interface is stucked!

    My way to avoid this problem is using something like the
    tipical Visual Basic "DoEvents" function. DoEvents tells to
    the application to handle the events (or messages) runnings
    instead of sleeping.

    All you have to do is include the "time.h" header file,
    because both "clock()" and "clock_t" are defined in that
    unit.

    void DoEvents(void)
    {
    MSG msg;

    while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    //-----------------------------------------------
    void Delay(int sec)
    {
    clock_t Start, End;

    Start=clock();
    End=Start+sec*CLK_TCK;

    while (clock()<End)
    {
    DoEvents();
    }

    }

    //--------------
    I hope this helps you.
    Alex


    Reply
  • Count FrameRate precisely

    Posted by Legacy on 04/04/2003 12:00am

    Originally posted by: David Lai

    Hi! Everybody,

    I want to control my application output each frame I captured, and let them work like TV. I tried to use GetTickCount() and WM_TIMER, however it was too fast, is there any kind of solutions? Thank you!

    Reply
  • Bad usage of CPU resources, use API Call ::Sleep() or ::SleepEx() instead

    Posted by Legacy on 01/13/2002 12:00am

    Originally posted by: OReubens

    Actually, using a for-loop to WAIT is a bad idea. The wait in this way has the CPU cycle around until the time is passed, it eats up valuable CPU cycles that other processes/threads might put to good use...
    Calling the Windows API function Sleep() or SleepEx() will have a millisecond delay, and it will yield whatever CPU cycles to other threads during the wait.

    Reply
  • bad results with multimedia timers

    Posted by Legacy on 05/31/1999 12:00am

    Originally posted by: Filipe Campos

    Hi.
    
    with a pentium 133 mhz , i don�t have very good result�s.
    with the following code ,
    #include "windows.h"
    #include <stdio.h>
    #include "mmsystem.h"

    void CALLBACK TimerProc(UINT uiID,UINT uiMsg,DWORD dwUser,DWORD dw1,DWORD dw2);
    int i;
    DWORD dwTick;
    MMRESULT mRes ;

    void main(void)
    {

    printf("\n MMTimer ");

    dwTick = 0;
    mRes = timeSetEvent(100,1,&TimerProc,0,TIME_PERIODIC);

    Sleep(1000);

    mRes = timeKillEvent(mRes);

    Sleep(5000);
    }

    void CALLBACK TimerProc(UINT uiID,UINT uiMsg,DWORD dwUser,DWORD dw1,DWORD dw2)
    {
    dwTick++;
    printf("\n called at %d",GetTickCount());
    }

    the results were ...

    called at 5663002
    called at 5663002
    called at 5663002
    called at 5663002
    called at 5663022
    called at 5663123
    called at 5663223
    called at 5663323
    called at 5663423
    called at 5663523
    called at 5663623
    called at 5663724
    called at 5663824
    called at 5663924

    does anyone knows how to get this better?
    regards,

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds