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. Help with MFC Timers

Help with MFC Timers

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
6 Posts 4 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.
  • W Offline
    W Offline
    waxie
    wrote on last edited by
    #1

    Hi guys, I tried making my own timer application, where a number increments every second. I just wonder how SetTimer() and OnTimer() works? My timer class really does not work! X| It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.:(( How does this really work? I would really appreciate it if someone can give me a simple but concrete example. Yes, there are some samples in the internet, but really, I could not understand the mechanics beneath them. Can someone provide me with simple steps on creating this simple timer class? If I could only make this work.. my timer class is super essential to make everything in my application work. To the person who could help me with this, kudos to you! :cool: Many thanks, :rose:Christina

    P R 2 Replies Last reply
    0
    • W waxie

      Hi guys, I tried making my own timer application, where a number increments every second. I just wonder how SetTimer() and OnTimer() works? My timer class really does not work! X| It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.:(( How does this really work? I would really appreciate it if someone can give me a simple but concrete example. Yes, there are some samples in the internet, but really, I could not understand the mechanics beneath them. Can someone provide me with simple steps on creating this simple timer class? If I could only make this work.. my timer class is super essential to make everything in my application work. To the person who could help me with this, kudos to you! :cool: Many thanks, :rose:Christina

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      waxie wrote:

      It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.

      Can you show how you have initialized it? Simple example would be like this

      BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
      //{{AFX_MSG_MAP(CMainFrame)
      ON_WM_CREATE()
      ON_WM_TIMER()
      //}}AFX_MSG_MAP
      END_MESSAGE_MAP()

      //Create timer in OnCreate
      int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
      {
      if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
      return -1;
      //some other stuff
      SetTimer(1,1000,NULL);
      return 0;
      }
      //handler
      void CMainFrame::OnTimer(UINT nIDEvent)
      {
      CFrameWnd::OnTimer(nIDEvent);
      //handle
      }

      Prasad Notifier using ATL

      1 Reply Last reply
      0
      • W waxie

        Hi guys, I tried making my own timer application, where a number increments every second. I just wonder how SetTimer() and OnTimer() works? My timer class really does not work! X| It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.:(( How does this really work? I would really appreciate it if someone can give me a simple but concrete example. Yes, there are some samples in the internet, but really, I could not understand the mechanics beneath them. Can someone provide me with simple steps on creating this simple timer class? If I could only make this work.. my timer class is super essential to make everything in my application work. To the person who could help me with this, kudos to you! :cool: Many thanks, :rose:Christina

        R Offline
        R Offline
        Ranjan Shrestha
        wrote on last edited by
        #3

        For your using OnTimer() you need to inherit your class from CWnd base class. // Header file should look like this class CMyTimer: public CWnd { ... afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP() }; // Source file should look like this .... BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP() void CMyTimer::OnTimer(UINT nIDEvent) { //Todo } Do not forget to create a timer object after construction the object i.e., any where in the program you should - CMyTimer *pTimer = new CTimer; pTimer->Create(......); pTimer->SetTimer(.....); Your Timer should then work.

        K W 2 Replies Last reply
        0
        • R Ranjan Shrestha

          For your using OnTimer() you need to inherit your class from CWnd base class. // Header file should look like this class CMyTimer: public CWnd { ... afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP() }; // Source file should look like this .... BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP() void CMyTimer::OnTimer(UINT nIDEvent) { //Todo } Do not forget to create a timer object after construction the object i.e., any where in the program you should - CMyTimer *pTimer = new CTimer; pTimer->Create(......); pTimer->SetTimer(.....); Your Timer should then work.

          K Offline
          K Offline
          Kurt _B
          wrote on last edited by
          #4

          Why would you create a whole class just for a timer? Seems to be overkill.

          W 1 Reply Last reply
          0
          • K Kurt _B

            Why would you create a whole class just for a timer? Seems to be overkill.

            W Offline
            W Offline
            waxie
            wrote on last edited by
            #5

            Kurt _B wrote:

            Why would you create a whole class just for a timer? Seems to be overkill.

            LOL. :) Good point. Just for testing - because my timer in my application is not working. I finally figured it out! ;) :rose:Christina

            1 Reply Last reply
            0
            • R Ranjan Shrestha

              For your using OnTimer() you need to inherit your class from CWnd base class. // Header file should look like this class CMyTimer: public CWnd { ... afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP() }; // Source file should look like this .... BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP() void CMyTimer::OnTimer(UINT nIDEvent) { //Todo } Do not forget to create a timer object after construction the object i.e., any where in the program you should - CMyTimer *pTimer = new CTimer; pTimer->Create(......); pTimer->SetTimer(.....); Your Timer should then work.

              W Offline
              W Offline
              waxie
              wrote on last edited by
              #6

              Ranjan Shrestha wrote:

              BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP()

              My problem was with (above). How stupid of me. :mad: I finally got it. I overlooked my code. Thanks ranjan! :) :rose:Christina

              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