Windows timer callback functions

Posted on 1998-09-01
Last Modified: 2013-12-03
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


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




Question by:cjl036
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
  • 2

Accepted Solution

Answers2000 earned 200 total points
ID: 1414111
You need to make it a static member.  Full answer in the comment as it will take me some time to type...

Expert Comment

ID: 1414112
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

Hope this helps

Expert Comment

ID: 1414113
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);


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

2. Change this
SetTimer(ID_TIMER_1, 5000, this->TimerTimeout);
SetTimer(ID_TIMER_1, 5000, TimerTimeout);
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.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

726 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