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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • If you need new tools and tricks to make your meetings profitable and productive, then 5 Tips in 5 Minutes: A Quick Guide for More Profitable Sales Meetings is for you. Timely, practical tips that you can incorporate in just seconds will save you literally hours in travel and meeting time, not to mention help you to focus on what your sales prospects really want to know and how you can meet their needs. Get 5in5: A Quick Guide for More Profitable Sales Meetings and start building your sales the smarter, faster …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds