SetTimer() for delay

Posted on 1998-11-01
Last Modified: 2013-12-26

   I want to write a delay function down to 1ms. Currently, I use SetTimer(timerId, timeout) to do this. Problem is, the timming seems correct only for "timeout > 100". Why is this happening, my code is like

class NewDialog: public TDialog

    int timenow;
    void EvTimer(uint);



    timenow = 0;

void NewDialog::SetupWindow()
    SetTimer(1, 1);

void NewDialog::EvTimer(uint)
    if(timenow++ == 1000)
        MessageBox("Time out");
        timenow = 0;

I compiled the code with Borland C++ 5.02.
The MessageBox takes > 10s to appear. What's wrong?
Or, is there any other ways to implement delay() under Win32?

Any help will be appreciated.

Question by:TanTT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

gatkinso earned 70 total points
ID: 1299800
The Windows SetTimer function only has a resolution of about 11 ms.  For a higher resolution timer, I would suggest using an MCI timer (harder to do, and I am only about 50% sure how to do it), or perhaps spawn a "Timer" thread in which you loop continuously, sending a message every millisecond:

void TimerThreadProc(LONG lParam) {
 while (bRunning) {
  Sleep(1);  //Sleep() has a resolution of 1 ms (so "they" say)

I hope that this is not a real time application (otherwise you wouldn't be running Windows :)  ), as this code will be off a few microseconds in either direction.

For the MCI method, search the documentation for info on the following fragment:

              MCI_SETTIMECODE,    //  <--- this is the key
              DWORD dwFlags,
              (DWORD) (LPMCI_GENERIC_PARMS) lpSetTimeCode);
LVL 14

Expert Comment

ID: 1299801
Yes, gatkinso right, must use Pause command, but
must pay attention to 2 punkts:
1. This works only in thread !!!
2. You can not use Timer, but make Loop in you thread
 and Pause. For more details see Petzold's Book
"Programming in Windows"
This is example from 14 chapter:(multi2)

void Thread1 (PVOID pvoid)
     PPARAMS pparams ;

     pparams = (PPARAMS) pvoid ;

     while (!pparams->bKill)
             .... //you code
     _endthread () ;
Regards, Alex


Author Comment

ID: 1299802
I checked the accuracy of Sleep() with following code:

   for(count = 0; count < 10000; count)

The messagebox took more than 20s to appear. Thus, it won't be accurate if I implement the code as stated.

The MCI part, however, I didn't test it since a lot of codes have to be modified.

My friend told me to use QueryPerformanceCounter() & QueryPerformanceFrequency() for high-resolution timer. The functions worked quite well. The messagebox took exactly 10s to appear on screen.

Thanks anyway.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Clean URL from GET variables 2 123
SAMI File <SYNC Start = (number of millisecond)> Question 3 94
Web Development Tools for MVC 3 125
Getting attribute Values using xslt 4 65
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question