Microsecond delay

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • Learn which Mobile Development Platforms scored the highest in the current Forrester Wave. This report details the research findings about how well vendors fulfilled the 32 criteria to help you select the right partner for your mobile development platform needs. Download a free copy of the report compliments of Kinvey, a Progress company.

  • In today's world of connecting mainframes ("Big Iron") to "Big Data" for advanced business analytics, mainframes are connected to mobile and Internet of Things (IoT) devices, clouds, and other open systems, and therefore subject to external attacks just like distributed systems. At the same time, mainframes are also subject to internal attacks such as employee's malicious intents or negligence. Read this eBook to learn how machine learning can help to secure mainframe systems and improve operational …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date