Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delay function in VC++

Posted on 1998-08-27
6
Medium Priority
?
970 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
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 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
Industry Leaders: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

885 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