Solved

Delay function in VC++

Posted on 1998-08-27
6
962 Views
Last Modified: 2008-02-01
I use Sleep(int mSEC)for a short delay in win'95/98 MFC programming. However, this function is not very accurate. For example, when I say Sleep(5); sometimes I get 5 msec
delay and sometimes 4 or 7 msec delay. Does anybody know any better delay functions in VC++5? Thanks.
0
Comment
Question by:tsauy
[X]
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
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1171430
You can use the multi-media timer functions, Like TimeSetEvent()
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171431
For example the timer notification procedure

void CALLBACK TimPrc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1 DWORD dw2)
{
}

would be called by the multi-media timer set by

TimeSetEvent(5,0,TimPrc,0,TIM_ONEESHOT);
0
 

Author Comment

by:tsauy
ID: 1171432
nietod, I also heard that timeSetEvent() is better but never made it work in my program. I modified your code above a little bit as follows:

void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// nothing here
}

void Timer::SetTimer()
{
     timeSetEvent(5, 0, TimeProc, (DWORD) this, TIME_ONESHOT);
}

I also remembered to include "mmsystem.h" and link with winmm.lib. However, when I compiled the program, the compiler complained that "error C2664: 'timeSetEvent' : cannot convert parameter 3 from 'void (unsigned int,unsigned int,unsigned long,unsigned long,unsigned long)' to 'void (__stdcall *)(unsigned int,unsigned int,unsigned long,unsigned long,unsigned long)'".

I don't understand why my callback function is different. Any hint?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1171433
Note in the following that there are two procedures F1 and F2.

class SomeClass
{
   void F1(int i);
}

void F2(int i);

These two procedures seem to be the same, that is, they seem to have the same calling conventions, same parameters, same return value etc.  so you would think that a pointer could be made to point to either of them.  Not so.  They are different.  The function F2 takes only one parameter, and int.  The function F1, takes two parameters.  The second parameter is a hidden parameter.  It is a pointer to the object that F1 can work on.  You can reference this parameter in the function, it is the famous "this" pointer.  You don't have to (aren't allowed to) declare it however.

Thus the timer procedure you are trying to use has the wrong type.  It takesan additional parameter that the LPTIMECALLBACK type doesn't.  

The way around this is with an "interface" function.  This is a regular function that doesn't take the object pointer as a hidden parameter, but instead takes it as a regular parameter.  Then this procedure uses the pointer to call the member procedure.

continues

0
 
LVL 22

Expert Comment

by:nietod
ID: 1171434
You could do

void CALLBACK InterfaceProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
     Timer *TimPtr = (timer *) dwUser;
     TimPtr->TimeProc(uID,uMsg,dwUser,dw1,dw2);
}

void Timer::SetTimer()
{
    timeSetEvent(5, 0, InterfaceProc, (DWORD) this, TIME_ONESHOT);
}

Note that the pointer to the object to be used is passed in the "user" parameter.  You were already doing this for some reason.  This pointer comes into the interface procedure as a DWORD and must be cast back to an object pointer, then the object's member procedure is called.

If you wanted, the inteface procedure could be a static member function of the class.  A static member function doesn't take the hidden this pointer parameter.
0
 

Author Comment

by:tsauy
ID: 1171435
Thanks nietod, I finally made the program work. After reading your posting carefully, I realize that I should assign the function as static, shown as follows:

static void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD
     dw1, DWORD dw2)
     {
     // nothing here
     }

     void Timer::SetTimer()
     {
          timeSetEvent(5, 0, TimeProc, (DWORD) this, TIME_ONESHOT);
     }

Then the compiler stops complaining. :-)

To make TimeProc() do something serious, I made it call another function MoreSerious():

static void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD
     dw1, DWORD dw2)
{
     Timer *TimPtr = (timer *) dwUser;
     TimPtr->MoreSerious();
 }

void Timer::MoreSerious()
{
     //Put your code here!
}

Unfortunately, timeSetEvent() is not as accurate as I expected. It has at least +/- 1 msec error range and is affected by moving mouse(?) or other things. I hope that nietod's posting and my experience will help other people who want to use timeSetEvent(). Good Luck!
0

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

624 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