Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generic TimerProc

Posted on 1998-03-11
3
Medium Priority
?
658 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 how to clear a vector as well as how to detect empty vectors in C++.

609 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