Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delay function in VC++

Posted on 1998-08-27
6
Medium Priority
?
965 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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 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++.

688 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