Link to home
Start Free TrialLog in
Avatar of cjl036
cjl036

asked on

Windows timer callback functions

I wnat to setup several timers (one for each instance of an object that I will dynamically create) and have a member function of that object be used as the callback function. However when I try to to this I get a compiler error as follows:

error C2664: 'SetTimer' : cannot convert parameter 3 from 'void (struct HWND__ *, unsigned int, unsigned int, unsigned long)' to 'void (__stdcall *)(struct HWND__ *, unsigned int, unsigned int, unsigned long)'

The Win32 documentation says to declare the callback function like this :

VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime );

which I have done to no avail.

My question is how can I use a member function as a timer callback?




Some Of my code:


void CMyClass::StartTimer()
{
      SetTimer(ID_TIMER_1, 5000, this->TimerTimeout);
}

void CMyClass::TimerTimeout(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
     if (TRUE == DoStateMachine(m_currentState))
     {
          m_currentState ++;
     }
}

class CMyClass
{
     // Declarations and stuff here
              .
              .
              .

public:

     void CALLBACK TimerTimeout( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);


     .
     .
     .

};
 


Thanks,

Joe
ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snoegler
snoegler

1st: A pointer to a member function of a class must be declared as 'static'
       This is because windows don't know which class pointer belongs to the member function
       pointer you passed - there is no easy workaround to that. Don't do it - that is the
       easiest way.

I would really recommend that you don't use timers for this purpose. Instead, use
worker threads - they are easier to handle.

You could do it like this:

UINT MyTimerThread(LPVOID pParam)
{
  CMyClass *pThis=(CMyClass*)pParam; // <-- by this you have access to your class
 
  for(;;) {
    Sleep(dwTimeoutMsec); // <-- use your time delay here
    // remember: Sleep() does *not* eat processor time - the time is used by other threads
   
    ... <-- your code
  }
  return 0;
}

You may think that this possibility is not very accurate, but the windows timer has some
disadvantages as well:
- it isn't guaranteed to be delivered
- if it is delivered, the delay you specified is just a 'wish' - windows may delay it as it likes
  depending on the processor time available.
If you are using worker threads, then the only disadvantage (at least how i showed it above)
is that the delay adds to the time your processing consumes. But there are ways to overcome
this - i don't explain them here(if you need more information, just post a comment, there are
enough people here to answer)

To create the worker thread ( I am assuming that you use MFC because of
this CMyClass() and SetTimer() things):

  AfxBeginThread( (AFX_THREADPROC)MyTimerThread, (LPVOID) pClass);

'pClass' refers in the example i gave to the pointer of the class which should be passed to
your worker thread.

Alternatively you can use a CWinThread() derived class, which is possibly easier to use in
for you ( see the docs).
Another thing:
Instead of using SetTimer() and passing a pointer to a callback function, why don't you
simply use SetTimer(ID_TIMER_1,5000,NULL) and then catch the WM_TIMER events:

CMyClass::OnTimer(int nIDEvent)
{
  switch(nIDEvent) {
    case ID_TIMER_1:
      ... // do some processing
      break;
    ...
  }
}

Hope this helps
The reason for this is that all non-static member functions of C++ classes include an extra hidden parameter (this pointer).  Therefore your declaration of TimerTimeout doesn't match what Windows expects for the function.

1. Change this
void CALLBACK TimerTimeout( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);

to

static void CALLBACK TimerTimeout( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);

2. Change this
SetTimer(ID_TIMER_1, 5000, this->TimerTimeout);
to
SetTimer(ID_TIMER_1, 5000, TimerTimeout);
or
SetTimer(ID_TIMER_1, 5000, CMyClass::TimerTimeout);

3. Now the problem you have left is that the static member function can't access the member data of the object to which it belongs.  There are a number of ways round this

General (not helpful in this particular case)
i. A lot of Win32 functions allow you to pass a long or void * into a callback routine.  You can use this to pass the value of 'this' into the callback routine.  Unfortunately SetTimer lacks this functionality.  So in this case this is no help.

ii. If you have one timer per hWnd, a good way round this is to stick a copy of the this pointer into a window property or extra window long before creating the timer, and then retrieve it in the timer proc.

iii. The most general solution is to add a CMap of timer-id's to the associated objects.  Add to the CMap when you create a timer, and remove from it when you kill it.  Provided the CMap is static, the TimerProc can then use the map to look up a pointer to its owning object.