Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Timer Callback

Timer Callback

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L LittleYellowBird

    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

    R Offline
    R Offline
    Ryan Binns
    wrote on last edited by
    #7

    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"

    I P L 3 Replies Last reply
    0
    • U User 423850

      this thing will work if u calling the set timer function in the view class itself.. Thanx TAKE CARE

      L Offline
      L Offline
      LittleYellowBird
      wrote on last edited by
      #8

      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

      1 Reply Last reply
      0
      • R Ryan Binns

        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"

        I Offline
        I Offline
        ivor bigun
        wrote on last edited by
        #9

        Haha, yes the declaration should be static void CALLBACK etc. well done ryan :)

        1 Reply Last reply
        0
        • R Ryan Binns

          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"

          P Offline
          P Offline
          Phil Speller
          wrote on last edited by
          #10

          To expand on Ryan's post - it's all to do with pointers to member functions. Take a look at this[^] site - it will explain all! Phil

          L 1 Reply Last reply
          0
          • P Phil Speller

            To expand on Ryan's post - it's all to do with pointers to member functions. Take a look at this[^] site - it will explain all! Phil

            L Offline
            L Offline
            LittleYellowBird
            wrote on last edited by
            #11

            Thanks, the link looks really helpful, looks like I need to do some reading! :) Ali

            1 Reply Last reply
            0
            • R Ryan Binns

              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"

              L Offline
              L Offline
              LittleYellowBird
              wrote on last edited by
              #12

              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

              R P 2 Replies Last reply
              0
              • L LittleYellowBird

                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

                R Offline
                R Offline
                Ryan Binns
                wrote on last edited by
                #13

                You'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"

                1 Reply Last reply
                0
                • L LittleYellowBird

                  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

                  V Offline
                  V Offline
                  Vitali Halershtein
                  wrote on last edited by
                  #14

                  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.

                  R 1 Reply Last reply
                  0
                  • L LittleYellowBird

                    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

                    P Offline
                    P Offline
                    Phil Speller
                    wrote on last edited by
                    #15

                    Declaring a member function of class as static will remove the need for an instance of the class to be associated with it - i.e. no this pointer. What this means is that it can't access any of the normal member functions/variables - precisely because there is no associated this pointer. However, supply the static function with a pointer to an instance of the class and you can access all functions/variables (even private ones) via that pointer - because the static function is still declared in the scope of the class. In terms of pointers to functions, because a static member function has no associated this 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 associated this 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 the nIDEvent parameter of SetTimer 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

                    1 Reply Last reply
                    0
                    • V Vitali Halershtein

                      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.

                      R Offline
                      R Offline
                      Ryan Binns
                      wrote on last edited by
                      #16

                      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"

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups