Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Class-member-function as TimerProc

Posted on 2000-04-13
4
Medium Priority
?
1,029 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 200 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

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

876 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