Solved

Class-member-function as TimerProc

Posted on 2000-04-13
4
992 Views
Last Modified: 2009-07-29
Hi,

I can't figure out how to set a timer so
that it calls a member-function of my
class.  

class SomeClass
{
   public:
   void CALLBACK EXPORT MyTimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime);
   void SomeFunction();
}

void SomeClass::SomeFunction()
{
   SetTimer(...,MyTimerProc);  // << error C2664
}

Currently all I can do is to use a
global, - non-class-member-function
but I need to have access to my class'es
variables when the timer-event occurs.

My problem is that when I try to use
a class-member-function in my SetTimer-
call then the compiler (VC 5.0) refuses
with:
error C2664:
'SetTimer' : cannot convert parameter 4 from 'void (struct HWND__ *,unsigned int,unsigned int,unsigned long)' to 'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)'


Basically I want to stay "within" my
class with the TimerProc.  Seemingly
I need a cast of some sort.

How can I do that ?  Any help greatly appreciated !  Thanks a lot !
Richard
0
Comment
Question by:rhoepperger
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
ufolk123 earned 50 total points
ID: 2711095
Hi rhoepperger,

A class member can not be used as an argument to ordinary callbacks etc because all class member functions( other than static ) assume one extra parameter( this ) to be passed hence can not be used for funcitons with normal signatures.
You can do the following.
Create a ordinary wrapper function f.
Now pass the object pointer op as a paramter to f.
Call the function inside the f as a normal class function using
op->actual function

Use the f as argument to timer callbacks.
Get back in case of any doubts,.

0
 

Author Comment

by:rhoepperger
ID: 2711130
Thanks a lot.  Good solution.
But I just came up with another solution:

MMRESULT mmres=timeSetEvent(1000, 100, TimeProc, (DWORD) this, TIME_PERIODIC);

Multimedia-Timers accept a user-argument
and a point to my this will do the job
for me I guess.

Thanks for your help !
Get the points and the A :))
Enjoy
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2711180
Yes , it will work
Only thing here is that  this solution will restrict the callback function to a  function taking no parameters,so you will not be able to use a function with one or more parameters as a timer callback.

0
 

Expert Comment

by:ananthag
ID: 8544771
Ufolk123,
I am interested in your solution to the problem as I can't use a timer that runs in its own thread. Can you send a brief outline of what you have mentioned.
Thanks in advance,
Ananthag
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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