Solved

Windows timer callback functions

Posted on 1998-09-01
3
967 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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