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. Using timers

Using timers

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 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.
  • M Offline
    M Offline
    minkowski
    wrote on last edited by
    #1

    Hi, I am a bit confused on how to make and use a timer. Currently I have void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); } where void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!

    N C A 3 Replies Last reply
    0
    • M minkowski

      Hi, I am a bit confused on how to make and use a timer. Currently I have void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); } where void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!

      N Offline
      N Offline
      Navaid Alam
      wrote on last edited by
      #2

      OnTimer() is not supposed to be called directly, rather when the timer expires, your app will receive a message and this handler will get called. If you have more than one timer, the nIDEvent should be used to identify it. This is the same value that you passed to SetTimer() as the first argument. So you should specify different values for the first param for each timer attached to a particular window.

      M 1 Reply Last reply
      0
      • M minkowski

        Hi, I am a bit confused on how to make and use a timer. Currently I have void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); } where void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        I'm not sure if I understood correctly your question. Anyway, you don't have to call OnTimer yourself. This function will be called by the framework when a specific timer fires. The framework will pass in the nIDEvent parameter the Id of the timer that fired. So, for all the timers that expire, you will have only one OnTimer function. In the function, it is up to you to do some specific things depending of the timer (you could use a switch statement and do several different things depending on the Id you receive). Hope that helps.


        Cédric Moonen Software developer
        Charting control [v1.2]

        M 2 Replies Last reply
        0
        • N Navaid Alam

          OnTimer() is not supposed to be called directly, rather when the timer expires, your app will receive a message and this handler will get called. If you have more than one timer, the nIDEvent should be used to identify it. This is the same value that you passed to SetTimer() as the first argument. So you should specify different values for the first param for each timer attached to a particular window.

          M Offline
          M Offline
          minkowski
          wrote on last edited by
          #4

          Hey thanks for your reply. So in my class where I have defined the function void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } you're saying it is invoked when the time expires as defined in the SetTimer() function (in my case 2000ms). I put a break point in the function above but unfortunately it was not hit... errr isn't it supposed to be if the function is invoked after the 2000ms? Thanks.

          N 1 Reply Last reply
          0
          • C Cedric Moonen

            I'm not sure if I understood correctly your question. Anyway, you don't have to call OnTimer yourself. This function will be called by the framework when a specific timer fires. The framework will pass in the nIDEvent parameter the Id of the timer that fired. So, for all the timers that expire, you will have only one OnTimer function. In the function, it is up to you to do some specific things depending of the timer (you could use a switch statement and do several different things depending on the Id you receive). Hope that helps.


            Cédric Moonen Software developer
            Charting control [v1.2]

            M Offline
            M Offline
            minkowski
            wrote on last edited by
            #5

            Hey thanks for that. Yes, I was unsure whether I should have invoked the OnTimer() function or if windows does itself.

            1 Reply Last reply
            0
            • C Cedric Moonen

              I'm not sure if I understood correctly your question. Anyway, you don't have to call OnTimer yourself. This function will be called by the framework when a specific timer fires. The framework will pass in the nIDEvent parameter the Id of the timer that fired. So, for all the timers that expire, you will have only one OnTimer function. In the function, it is up to you to do some specific things depending of the timer (you could use a switch statement and do several different things depending on the Id you receive). Hope that helps.


              Cédric Moonen Software developer
              Charting control [v1.2]

              M Offline
              M Offline
              minkowski
              wrote on last edited by
              #6

              Hi, So in my class where I have defined the function void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } I put a break point in the function above but unfortunately it was not hit... errr isn't it supposed to be if the function is invoked after the 2seconds which was defined in my SetTimer()? Thanks for any advice.

              1 Reply Last reply
              0
              • M minkowski

                Hi, I am a bit confused on how to make and use a timer. Currently I have void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); } where void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!

                A Offline
                A Offline
                Abhijeet Pathak
                wrote on last edited by
                #7

                hi, the OnTimer() function is not supposed to be called directly... http://www.codeproject.com/system/timers_intro.asp this will be helpful to you... :)

                1 Reply Last reply
                0
                • M minkowski

                  Hey thanks for your reply. So in my class where I have defined the function void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); } you're saying it is invoked when the time expires as defined in the SetTimer() function (in my case 2000ms). I put a break point in the function above but unfortunately it was not hit... errr isn't it supposed to be if the function is invoked after the 2000ms? Thanks.

                  N Offline
                  N Offline
                  Navaid Alam
                  wrote on last edited by
                  #8

                  yes it should happen after 2s, if its not happening perhaps you have not set up the message map for WM_TIMER?

                  M 1 Reply Last reply
                  0
                  • N Navaid Alam

                    yes it should happen after 2s, if its not happening perhaps you have not set up the message map for WM_TIMER?

                    M Offline
                    M Offline
                    minkowski
                    wrote on last edited by
                    #9

                    Yes, that was the problem. I needed ON_WM_TIMER() Thanks for your help.

                    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