Solved

SetTimer() for delay

Posted on 1998-11-01
3
700 Views
Last Modified: 2013-12-26
Hi,

   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
this(simplified):


class NewDialog: public TDialog
{
public:
    NewDialog();
    ...
    ...

protected:
    int timenow;
    void EvTimer(uint);

    DECLARE_RESPONSE_TABLE(NewDialog);
};

DEFINE_RESPONSE_TABLE1(NewDialog, TDialog)
    EV_WM_TIMER,
END_RESPONSE_TABLE;

NewDialog::NewDialog()
{
    timenow = 0;
}

void NewDialog::SetupWindow()
{
    TDialog::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.

Thanks.
0
Comment
Question by:TanTT
3 Comments
 

Accepted Solution

by:
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)
  SendMessage(hTargetWnd,ID_YOURTIMEOUTMESSAGEID,lYourDataParam);
 }
}

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:

MCIERROR mciSendCommand(MCIDEVICEID wDeviceID,
              MCI_SETTIMECODE,    //  <--- this is the key
              DWORD dwFlags,
              (DWORD) (LPMCI_GENERIC_PARMS) lpSetTimeCode);
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
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
              Pause(1);
          }
     _endthread () ;
     }
Regards, Alex

0
 

Author Comment

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

   for(count = 0; count < 10000; count)
      Sleep(1);
   MessageBox("Timeout");

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.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to orderly load jquery libs with requireJS? 3 64
Magento Indexing 1 73
Form submit issue 11 55
Swiper slider stops between pages 11 32
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now