?
Solved

Generic TimerProc

Posted on 1998-03-11
3
Medium Priority
?
655 Views
Last Modified: 2006-11-17
How can I declare a generic timerProc such that when an object is declared the timer proc would know which object (assuming the objects are instances of the same class) to notify?
My preferred method is to declare a class and declare a timer proc within that class so every object knows its timer proc, and every timer proc knows the object to notify which would be the object it belongs to. However, this is not possible since Windows TimerProc that the timer function uses is written for C and thus does not expect the "this" pointer.

This is an example of what I have in mind:

class MovingObject
{
public:
MovingObjec() ;
CALLBACK TimerProc (HWND, ........);
void Animate () ;
}


 MovingObjec::MovingObjec()
{
 SetTimer (NULL, NULL, 100, TimerProc) ;
}

CALLBACK  MovingObjec::TimerProc (HWND.........)
{
Animate() ;
}

But the above code is invalid and generate a compiler error.
What would be the solution without writing a new timer proc for every new instance of this calss?


Thanks.

(sorry for the lengthy explanation!)
0
Comment
Question by:Zainal062797
[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
3 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1183375
If you want to have multiple timer (one for your each object), then you have to associate your object with its own window, this way when you call SetTimer, you can specify the window handle and an id for the timer.  The timerproc has to be out of the class, but it can check with the id and then finds the appropriate object (to do that you need a global array consisting the pointer to the object, and the timer id can be the array index) and therefore call a member function of the object.

I agree that it seems to be very inconvenient, but that is what you have to do.

Maybe you should reconsider you design.  I mean, use one timer to control all your objects.  Or if your object has nothing to do before the notification, then the simplest way is to call sleep() in your object's member function.
0
 
LVL 23

Accepted Solution

by:
chensu earned 600 total points
ID: 1183376
The Windows timers do not meet your requirement. Instead, you can use Multimedia Timers. It provides you with a timer ID and a 32-bit user data so that you can pass 'this' pointer to it. Put the TimeProc() as a static member function of your class. You may need to use the following functions.

timeGetDevCaps
timeBeginPeriod
timeSetEvent
timeKillEvent
timeEndPeriod

void CALLBACK TimeProc(UINT uID,
UINT uMsg,
DWORD dwUser,
DWORD dw1,
DWORD dw2  
);

Import Library winmm.lib
 
Header File  mmsystem.h

0
 
LVL 22

Expert Comment

by:nietod
ID: 1183377
Note chensu is suggesting that you have only one timer per class not one timer per object.  One timer per object is simpler to impliment, but can cause a big drain on system resources if you have more than just a few of these objects at a time.  On time per class, menas that each program uses only one timer and is much easier on the system (but harder on the programmer).  

What chensu left out was that if you use a one timer per class scheme, the class will need to mainatain a list of objects that are part of the class so that it can notify all the objects when it is notified by the timer.  This can easily be done using a linked list that is  modified by the object's constructors and destructors.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 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