Solved

Delay function in VC++

Posted on 1998-08-27
6
952 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
Comment Utility
You can use the multi-media timer functions, Like TimeSetEvent()
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

10 Experts available now in Live!

Get 1:1 Help Now