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. SetTimer doesn't work

SetTimer doesn't work

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 5 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.
  • U Offline
    U Offline
    User 11731824
    wrote on last edited by
    #1

    Hi All, I used the following code. But it appears that my code in the OnTimer function is never executed. The strangest thing is that if I click the menu ,so it drops down, the timer is executed! And as soon as I pull up the menu again, the timer stops! Can anyone give me a explaination for this? Thanks a lot. void CMyDialog::OnMyFunction() { ... SetTimer(1, 500, NULL); ... } void CMyDialog::OnTimer(UINT nIDEvent) { ... // my code} }

    D A 2 Replies Last reply
    0
    • U User 11731824

      Hi All, I used the following code. But it appears that my code in the OnTimer function is never executed. The strangest thing is that if I click the menu ,so it drops down, the timer is executed! And as soon as I pull up the menu again, the timer stops! Can anyone give me a explaination for this? Thanks a lot. void CMyDialog::OnMyFunction() { ... SetTimer(1, 500, NULL); ... } void CMyDialog::OnTimer(UINT nIDEvent) { ... // my code} }

      D Offline
      D Offline
      Dmitriy
      wrote on last edited by
      #2

      Have you a handler for WM_TIMER in message map? BEGIN_MESSAGE_MAP... ... ON_WN_TIMER() END_MESSAGE_MAP try to use VERIFY(SetTimer(1,500,NULL)); to make sure you've set timer

      U 1 Reply Last reply
      0
      • D Dmitriy

        Have you a handler for WM_TIMER in message map? BEGIN_MESSAGE_MAP... ... ON_WN_TIMER() END_MESSAGE_MAP try to use VERIFY(SetTimer(1,500,NULL)); to make sure you've set timer

        U Offline
        U Offline
        User 11731824
        wrote on last edited by
        #3

        Yes, I have the handler in message map. what does the VERIFY() should do? because nothing different happened when I add it. The line of code right before the SetTimer(1,500,NULL) is: MCIWnd(hMCI); // plays a movie This shouldn't affect anything, should it? I know that MSDN says "The WM_TIMER message is a low-priority message." I have another smaller app that also dialog-based that does that: plays a movie while activating the timer. Thanks.

        D 2 Replies Last reply
        0
        • U User 11731824

          Yes, I have the handler in message map. what does the VERIFY() should do? because nothing different happened when I add it. The line of code right before the SetTimer(1,500,NULL) is: MCIWnd(hMCI); // plays a movie This shouldn't affect anything, should it? I know that MSDN says "The WM_TIMER message is a low-priority message." I have another smaller app that also dialog-based that does that: plays a movie while activating the timer. Thanks.

          D Offline
          D Offline
          Dmitriy
          wrote on last edited by
          #4

          VERIFY does make sure the function returns value non-zero, otherwise it popups assertion dialog box in DEBUG, in Release does nothing. You, probably, right speaking about low-priority. You may create separate thread for handling WM_TIMER.

          U 1 Reply Last reply
          0
          • U User 11731824

            Yes, I have the handler in message map. what does the VERIFY() should do? because nothing different happened when I add it. The line of code right before the SetTimer(1,500,NULL) is: MCIWnd(hMCI); // plays a movie This shouldn't affect anything, should it? I know that MSDN says "The WM_TIMER message is a low-priority message." I have another smaller app that also dialog-based that does that: plays a movie while activating the timer. Thanks.

            D Offline
            D Offline
            Dmitriy
            wrote on last edited by
            #5

            VERIFY does make sure the function returns value non-zero, otherwise it popups assertion dialog box in DEBUG, in Release does nothing. You, probably, right speaking about low-priority. You may create separate thread for handling WM_TIMER.

            1 Reply Last reply
            0
            • D Dmitriy

              VERIFY does make sure the function returns value non-zero, otherwise it popups assertion dialog box in DEBUG, in Release does nothing. You, probably, right speaking about low-priority. You may create separate thread for handling WM_TIMER.

              U Offline
              U Offline
              User 11731824
              wrote on last edited by
              #6

              I never use thread before (and it sounds&seems confusing) :) Could you please post a small sample code on how to create, call, and destroy a simple thread on an MFC dialog-based? Thanks a lot in advance.

              1 Reply Last reply
              0
              • U User 11731824

                Hi All, I used the following code. But it appears that my code in the OnTimer function is never executed. The strangest thing is that if I click the menu ,so it drops down, the timer is executed! And as soon as I pull up the menu again, the timer stops! Can anyone give me a explaination for this? Thanks a lot. void CMyDialog::OnMyFunction() { ... SetTimer(1, 500, NULL); ... } void CMyDialog::OnTimer(UINT nIDEvent) { ... // my code} }

                A Offline
                A Offline
                Andy Bantly
                wrote on last edited by
                #7

                Try running it through the debugger and trace your code. Something might make itself clear to you. ~Andy

                U 1 Reply Last reply
                0
                • A Andy Bantly

                  Try running it through the debugger and trace your code. Something might make itself clear to you. ~Andy

                  U Offline
                  U Offline
                  User 11731824
                  wrote on last edited by
                  #8

                  I did some more digging and found something: In the project, I create an owner draw button. What I want to do is move this button while the movie is playing. Therefore I add this line on OnTimer: GetDlgItem(IDC_MY_IMAGE_BUTTON)->MoveWindow(newX, newY, newWidth, newHeight, true); And it works (it moves while the movie playing). But I also want to draw an image on the button, so I created a new class CMyImageButton, derived from CButton class. And as soon as I associated the owner draw button variable to CMyImageButton class, the OnTimer stop working. Any idea why? Thanks.

                  P 1 Reply Last reply
                  0
                  • U User 11731824

                    I did some more digging and found something: In the project, I create an owner draw button. What I want to do is move this button while the movie is playing. Therefore I add this line on OnTimer: GetDlgItem(IDC_MY_IMAGE_BUTTON)->MoveWindow(newX, newY, newWidth, newHeight, true); And it works (it moves while the movie playing). But I also want to draw an image on the button, so I created a new class CMyImageButton, derived from CButton class. And as soon as I associated the owner draw button variable to CMyImageButton class, the OnTimer stop working. Any idea why? Thanks.

                    P Offline
                    P Offline
                    Paul Hooper
                    wrote on last edited by
                    #9

                    Many moons ago I remember having all sorts of trouble with SetTimer and seem to remember thatI ended up fixing them by changing the event number ie SetTimer(111,500,NULL); I never worked out why this worked and 1 didn't but it may be worth a try in your case.

                    S 1 Reply Last reply
                    0
                    • P Paul Hooper

                      Many moons ago I remember having all sorts of trouble with SetTimer and seem to remember thatI ended up fixing them by changing the event number ie SetTimer(111,500,NULL); I never worked out why this worked and 1 didn't but it may be worth a try in your case.

                      S Offline
                      S Offline
                      Stephen Kellett
                      wrote on last edited by
                      #10

                      If you are using SetTimer() with a NULL 3rd argument you are relying on the message queue sending you the lowest priority message going to tell you about the timer. Not good. Even worse is if you are busy then no messages are being processed, hence your timer will never be triggered. This is why when you click the menu, you caused some processing of the message queue to happen, you get your timer event. Solution 1: Pass a callback function for parameter 3. This will be called much more reliably. Solution 2: Go multi threaded (beyond the scope of me typing just before going home). There are various articles on codeproject or you could take a look at some of the Platform SDK samples. I've used both 1 and 2 depending on the situtation (and programming model - Win16 only solution 1 is available). Cheers Stephen Kellett

                      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