Solved

Generic TimerProc

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

732 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