Solved

Class-member-function as TimerProc

Posted on 2000-04-13
4
977 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

708 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

16 Experts available now in Live!

Get 1:1 Help Now