Solved

Generic TimerProc

Posted on 1998-03-11
3
637 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.

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