Solved

Windows timer callback functions

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
link access with Quickbooks 9 49
Get the most recent file to copy into a sql table with ssis 2 103
Need example 5 113
Delphi Mdi application Child forms get behind control 7 212
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.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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