Solved

Windows timer callback functions

Posted on 1998-09-01
3
997 Views
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
              .
              .
              .

public:

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


     .
     .
     .

};
 


Thanks,

Joe
0
Comment
Question by:cjl036
[X]
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
3 Comments
 
LVL 8

Accepted Solution

by:
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...
0
 
LVL 6

Expert Comment

by:snoegler
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
      break;
    ...
  }
}

Hope this helps
0
 
LVL 8

Expert Comment

by:Answers2000
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);

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.





0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

628 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