Solved

Delay function in VC++

Posted on 1998-08-27
6
956 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

810 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