Timer Callback
-
Hi, I'm trying to use a timer call back function but I can't get it to compile. I set up the timer for a 1 second interval like this:-
m_uSampleTimerID = SetTimer( 1,1000,TimerTakeSample );
My function is declared like this:-void CALLBACK EXPORT COxygraphView::TimerTakeSample(HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTime)
BUT the SetTime command gives the following error:- :\Oxygraph\OxygraphView.cpp(575) : 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 l ong)' This is all in the View part of an MFC app. It doesn't seem to like the return type of the function. Any ideas what's wrong with it? Thanks Ali -
Hi, I'm trying to use a timer call back function but I can't get it to compile. I set up the timer for a 1 second interval like this:-
m_uSampleTimerID = SetTimer( 1,1000,TimerTakeSample );
My function is declared like this:-void CALLBACK EXPORT COxygraphView::TimerTakeSample(HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTime)
BUT the SetTime command gives the following error:- :\Oxygraph\OxygraphView.cpp(575) : 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 l ong)' This is all in the View part of an MFC app. It doesn't seem to like the return type of the function. Any ideas what's wrong with it? Thanks AliHey can i suggest u one thing.. Check out if u like this thing instead of calling a call back function just go to class wizard map the WM_TIMER for the view class then call the set timer function with parameters(1000, timeasperuwant, NULL) in this case the Functiopn mapped for WM_TIMER will be called.. please check out and let me know bye Thanx TAKE CARE
-
Hey can i suggest u one thing.. Check out if u like this thing instead of calling a call back function just go to class wizard map the WM_TIMER for the view class then call the set timer function with parameters(1000, timeasperuwant, NULL) in this case the Functiopn mapped for WM_TIMER will be called.. please check out and let me know bye Thanx TAKE CARE
this thing will work if u calling the set timer function in the view class itself.. Thanx TAKE CARE
-
Hi, I'm trying to use a timer call back function but I can't get it to compile. I set up the timer for a 1 second interval like this:-
m_uSampleTimerID = SetTimer( 1,1000,TimerTakeSample );
My function is declared like this:-void CALLBACK EXPORT COxygraphView::TimerTakeSample(HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTime)
BUT the SetTime command gives the following error:- :\Oxygraph\OxygraphView.cpp(575) : 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 l ong)' This is all in the View part of an MFC app. It doesn't seem to like the return type of the function. Any ideas what's wrong with it? Thanks AliThe problem is with the declaration of your TimerTakeSample function. I think its probably got something to do with that EXPORT (try removing it?) because the CALLBACK simply defines the function type as an _stdcall* and so that should compile.
-
Hi, I'm trying to use a timer call back function but I can't get it to compile. I set up the timer for a 1 second interval like this:-
m_uSampleTimerID = SetTimer( 1,1000,TimerTakeSample );
My function is declared like this:-void CALLBACK EXPORT COxygraphView::TimerTakeSample(HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTime)
BUT the SetTime command gives the following error:- :\Oxygraph\OxygraphView.cpp(575) : 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 l ong)' This is all in the View part of an MFC app. It doesn't seem to like the return type of the function. Any ideas what's wrong with it? Thanks AliThe actual problem is that the compiler is expecting a void (_stdcall*) (arglist...)function and your function has somehow been defined as a mere void (arglist...) function hence the error. Since the CALLBACK statement defines the (_stdcall*) you should not be having this issue, unless the EXPORT statement is doing something dopy (so try removing it), or the TimerTakeSample referred to in the SetTimer function is being linked to a different function? try specifying COxygraphView::TimerTakeSample(blah...) in your SetTimer function to make sure your linkage is correct (can't tell from your example where the SetTimer is being called from). I'm sure the answer will reveal itself :)
-
The problem is with the declaration of your TimerTakeSample function. I think its probably got something to do with that EXPORT (try removing it?) because the CALLBACK simply defines the function type as an _stdcall* and so that should compile.
Thanks for the suggestion, I've tried it but the compiler still doesn't want to know (still the same error message). Thanks anyway :) Ali
-
Hi, I'm trying to use a timer call back function but I can't get it to compile. I set up the timer for a 1 second interval like this:-
m_uSampleTimerID = SetTimer( 1,1000,TimerTakeSample );
My function is declared like this:-void CALLBACK EXPORT COxygraphView::TimerTakeSample(HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTime)
BUT the SetTime command gives the following error:- :\Oxygraph\OxygraphView.cpp(575) : 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 l ong)' This is all in the View part of an MFC app. It doesn't seem to like the return type of the function. Any ideas what's wrong with it? Thanks AliThe function must be declared as
static
Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
this thing will work if u calling the set timer function in the view class itself.. Thanx TAKE CARE
Thanks for the suggestion. In fact I have got my code working like that at the moment, but I have several timers and I wanted to assign each one its own handling function. Thanks anyway :) Ali
-
The function must be declared as
static
Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Haha, yes the declaration should be static void CALLBACK etc. well done ryan :)
-
The function must be declared as
static
Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Thanks, the link looks really helpful, looks like I need to do some reading! :) Ali
-
The function must be declared as
static
Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
You're right changing the funstionto
static
gets rid of the compiler error. However, now I have more errors calling functions from the timer function. Looks like I need to read up on static functions! Thanks for the help, much appreciated :) Ali -
You're right changing the funstionto
static
gets rid of the compiler error. However, now I have more errors calling functions from the timer function. Looks like I need to read up on static functions! Thanks for the help, much appreciated :) AliYou're welcome :) Alison Pentland wrote: However, now I have more errors calling functions from the timer function. Looks like I need to read up on static functions! Good idea ;) Your errors at the moment are probably because you're calling non-static members from your static function. You can't do that :). The general way of using static functions as callbacks is to pass a pointer to the object as the
lParam
value that gets passed to the callback function, and then using that pointer to access the other class members.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Hi, I'm trying to use a timer call back function but I can't get it to compile. I set up the timer for a 1 second interval like this:-
m_uSampleTimerID = SetTimer( 1,1000,TimerTakeSample );
My function is declared like this:-void CALLBACK EXPORT COxygraphView::TimerTakeSample(HWND hWnd, UINT nMsg, UINT nTimerID, DWORD dwTime)
BUT the SetTime command gives the following error:- :\Oxygraph\OxygraphView.cpp(575) : 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 l ong)' This is all in the View part of an MFC app. It doesn't seem to like the return type of the function. Any ideas what's wrong with it? Thanks AliHi Alison, I am not sure that "static" is right method. See following: The citation: -The lpfnTimer callback function need not be named TimerProc, but it must be defined as follows: -void CALLBACK EXPORT TimerProc( - HWND hWnd, // handle of CWnd that called SetTimer - UINT nMsg, // WM_TIMER - UINT nIDEvent // timer identification - DWORD dwTime // system time -); So, TimerProc should be a function insted class method.
-
You're right changing the funstionto
static
gets rid of the compiler error. However, now I have more errors calling functions from the timer function. Looks like I need to read up on static functions! Thanks for the help, much appreciated :) AliDeclaring a member function of class as
static
will remove the need for an instance of the class to be associated with it - i.e. nothis
pointer. What this means is that it can't access any of the normal member functions/variables - precisely because there is no associatedthis
pointer. However, supply thestatic
function with a pointer to an instance of the class and you can access all functions/variables (even private ones) via that pointer - because thestatic
function is still declared in the scope of the class. In terms of pointers to functions, because a static member function has no associatedthis
pointer you can obtain it's address directly (just like a C function) - this is why Ryan's suggestion worked. Any normal member function must have an associatedthis
pointer with it, so even when you take the address of a member function you must still use it with a pointer to an instance of the class as well.class CMyClass { public: void MyFn (void); static void MyStaticFn (CMyClass *); }; typedef void(* FnPtr)(CMyClass*); // C style fn pointer typedef void(CMyClass::* MemberFnPtr)(); // C++ style fn pointer CMyClass* pMyClass = new CMyClass; MemberFnPtr pMFn = &CMyClass::MyFn; pMyClass->MyFn(); // Calling as normal pMyClass->*pMFn(); // Calling via pointer to member fn FnPtr pFn = &CMyClass::MyStaticFn; (*pFn)( pMyClass ); // Calling static member fn
[edit] You could try using thenIDEvent
parameter ofSetTimer
to hold the pointer to your class instance - cast it back to the correct type and use within your static fn. [/edit] Hope this helps, Phil -
Hi Alison, I am not sure that "static" is right method. See following: The citation: -The lpfnTimer callback function need not be named TimerProc, but it must be defined as follows: -void CALLBACK EXPORT TimerProc( - HWND hWnd, // handle of CWnd that called SetTimer - UINT nMsg, // WM_TIMER - UINT nIDEvent // timer identification - DWORD dwTime // system time -); So, TimerProc should be a function insted class method.
Talik wrote: So, TimerProc should be a function insted class method. There is nothing wrong with using a static member. The compiler treats functions and static members in exactly the same way.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"