Solved

Generic TimerProc

Posted on 1998-03-11
3
641 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
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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

914 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

12 Experts available now in Live!

Get 1:1 Help Now