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. Create really long timers in MFC

Create really long timers in MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++
21 Posts 8 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.
  • C Offline
    C Offline
    cagespear
    wrote on last edited by
    #1

    I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

    I K C M Y 6 Replies Last reply
    0
    • C cagespear

      I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      When you start calculating 24 hours in milliseconds (86400000) then you do get big numbers. And 2^32 can only hold 49.7 days, so I think your reluctance is very wise. There are alternative ways though - have a look at SetWaitableTimer. You can also set it to call a function at a certain time / date, so it may be more useful to you. Good luck, Iain.

      Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

      C C 2 Replies Last reply
      0
      • C cagespear

        I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        It is Ok, but you misses a "0". If you have others timers than you better count up. Happy debugging by waiting 24 hours for the function :zzz: :zzz: :zzz:

        Greetings from Germany

        C 2 Replies Last reply
        0
        • I Iain Clarke Warrior Programmer

          When you start calculating 24 hours in milliseconds (86400000) then you do get big numbers. And 2^32 can only hold 49.7 days, so I think your reluctance is very wise. There are alternative ways though - have a look at SetWaitableTimer. You can also set it to call a function at a certain time / date, so it may be more useful to you. Good luck, Iain.

          Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

          C Offline
          C Offline
          cagespear
          wrote on last edited by
          #4

          well, actually your point is of great help to me. If it can store 49 days at max then I'll buy that as I only need it max for 24 hours which is considerably lesser. Thanks a ton! Cage

          I 1 Reply Last reply
          0
          • K KarstenK

            It is Ok, but you misses a "0". If you have others timers than you better count up. Happy debugging by waiting 24 hours for the function :zzz: :zzz: :zzz:

            Greetings from Germany

            C Offline
            C Offline
            cagespear
            wrote on last edited by
            #5

            :-D for the xtra '0' - Looks like I am already sleepy :zzz: :-D

            1 Reply Last reply
            0
            • C cagespear

              well, actually your point is of great help to me. If it can store 49 days at max then I'll buy that as I only need it max for 24 hours which is considerably lesser. Thanks a ton! Cage

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              Just be very careful - you have a strong risk of wraparound. Have a look at http://blogs.msdn.com/oldnewthing/archive/2005/05/31/423407.aspx[^] for a very useful explanation. Iain.

              Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

              1 Reply Last reply
              0
              • C cagespear

                I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Well, since the Elapse argument of the SetTimer can take a maximum of USER_TIMER_MAXIMUM (i.e. on my system, 0x7FFFFFFF milliseconds, that is about 25 days) I'm happy with it. Moreover it isn't such a difficult task to make longer intervals using shorter ones. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                C 1 Reply Last reply
                0
                • I Iain Clarke Warrior Programmer

                  When you start calculating 24 hours in milliseconds (86400000) then you do get big numbers. And 2^32 can only hold 49.7 days, so I think your reluctance is very wise. There are alternative ways though - have a look at SetWaitableTimer. You can also set it to call a function at a certain time / date, so it may be more useful to you. Good luck, Iain.

                  Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Iain Clarke wrote:

                  And 2^32 can only hold 49.7 days

                  Actually is the half ;P , see [^]. :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  C I 2 Replies Last reply
                  0
                  • C CPallini

                    Well, since the Elapse argument of the SetTimer can take a maximum of USER_TIMER_MAXIMUM (i.e. on my system, 0x7FFFFFFF milliseconds, that is about 25 days) I'm happy with it. Moreover it isn't such a difficult task to make longer intervals using shorter ones. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    C Offline
                    C Offline
                    cagespear
                    wrote on last edited by
                    #9

                    I hear you. I can make small timers, kill them and do 'settimer' repeatedly, but then being a lazy programmer - was thinking of skipping them and just have a one-shot timer :-D -Amit

                    C 1 Reply Last reply
                    0
                    • C CPallini

                      Iain Clarke wrote:

                      And 2^32 can only hold 49.7 days

                      Actually is the half ;P , see [^]. :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      C Offline
                      C Offline
                      cagespear
                      wrote on last edited by
                      #10

                      Still works for me :) hope it doesn't shrink :-D

                      1 Reply Last reply
                      0
                      • K KarstenK

                        It is Ok, but you misses a "0". If you have others timers than you better count up. Happy debugging by waiting 24 hours for the function :zzz: :zzz: :zzz:

                        Greetings from Germany

                        C Offline
                        C Offline
                        cagespear
                        wrote on last edited by
                        #11

                        Have put the timers now before leaving for weekend, hope eveything works good when I come back on Monday :)

                        1 Reply Last reply
                        0
                        • C cagespear

                          I hear you. I can make small timers, kill them and do 'settimer' repeatedly, but then being a lazy programmer - was thinking of skipping them and just have a one-shot timer :-D -Amit

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #12

                          cagespear wrote:

                          I can make small timers, kill them and do 'settimer' repeatedly

                          Counting isn't an option for you, is it?

                          cagespear wrote:

                          but then being a lazy programmer

                          No. It isn't an option. :-D

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          1 Reply Last reply
                          0
                          • C cagespear

                            I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

                            M Offline
                            M Offline
                            MANISH RASTOGI
                            wrote on last edited by
                            #13

                            You can store datetime when application starts. Set some timer interval and read cureent time each time when receive WM_TIMER message. If the difference between application starting time and current time matches your requirement, take desired action.

                            1 Reply Last reply
                            0
                            • C CPallini

                              Iain Clarke wrote:

                              And 2^32 can only hold 49.7 days

                              Actually is the half ;P , see [^]. :-D

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              I Offline
                              I Offline
                              Iain Clarke Warrior Programmer
                              wrote on last edited by
                              #14

                              Well, my sums are accurate! If you're going to cheat and read the docs and find that SetTimer can only do 2^31-1, then shame on you! In my defense I was thinking of GetTickCount and wraparound. There are lots of ways of skinning his particular cat. I certainly wouldn't do a 24hr SetTimer - what if people chage the clock? Twice a year that gets done for you, etc... Iain.

                              Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                              C 1 Reply Last reply
                              0
                              • I Iain Clarke Warrior Programmer

                                Well, my sums are accurate! If you're going to cheat and read the docs and find that SetTimer can only do 2^31-1, then shame on you! In my defense I was thinking of GetTickCount and wraparound. There are lots of ways of skinning his particular cat. I certainly wouldn't do a 24hr SetTimer - what if people chage the clock? Twice a year that gets done for you, etc... Iain.

                                Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #15

                                Iain Clarke wrote:

                                If you're going to cheat and read the docs and find that SetTimer can only do 2^31-1, then shame on you!

                                Shame on me! :-D

                                Iain Clarke wrote:

                                I certainly wouldn't do a 24hr SetTimer - what if people chage the clock? Twice a year that gets done for you, etc...

                                Do you think the timer would be affected (I really have no clue about but i suppose it shouldn't: it's a timer, not a clock!)?

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                I R 2 Replies Last reply
                                0
                                • C CPallini

                                  Iain Clarke wrote:

                                  If you're going to cheat and read the docs and find that SetTimer can only do 2^31-1, then shame on you!

                                  Shame on me! :-D

                                  Iain Clarke wrote:

                                  I certainly wouldn't do a 24hr SetTimer - what if people chage the clock? Twice a year that gets done for you, etc...

                                  Do you think the timer would be affected (I really have no clue about but i suppose it shouldn't: it's a timer, not a clock!)?

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

                                  I Offline
                                  I Offline
                                  Iain Clarke Warrior Programmer
                                  wrote on last edited by
                                  #16

                                  CPallini wrote:

                                  Do you think the timer would be affected

                                  I also doubt the timer would be affected - but its usefulness would depend on what the OP is trying to do. If it's "do something approximately a day later", then SetTimer would be fine, or checking GetTickCount on a more rapid timer. If it's "do something at the same time tomorrow, and the user will look at their clock and be puzzled why they started the program at 1pm and it's midday now..." then you need to do it a different way. It really depends on what (s)he's trying to do. Iain.

                                  Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                                  C 1 Reply Last reply
                                  0
                                  • I Iain Clarke Warrior Programmer

                                    CPallini wrote:

                                    Do you think the timer would be affected

                                    I also doubt the timer would be affected - but its usefulness would depend on what the OP is trying to do. If it's "do something approximately a day later", then SetTimer would be fine, or checking GetTickCount on a more rapid timer. If it's "do something at the same time tomorrow, and the user will look at their clock and be puzzled why they started the program at 1pm and it's midday now..." then you need to do it a different way. It really depends on what (s)he's trying to do. Iain.

                                    Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #17

                                    Well, timers and alarms are pretty different things, I suppose the OP knows about. But this is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. :rolleyes:

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    1 Reply Last reply
                                    0
                                    • C cagespear

                                      I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

                                      Y Offline
                                      Y Offline
                                      Yves
                                      wrote on last edited by
                                      #18

                                      With this small class you can create a timer up to 1193 Hours #pragma once #include <string> typedef void (*TIMER_PROC)(UINT idEvent, void* obj); class CTimer { public: enum eTIME_TYPE { eONE_SHOT, ePERIOIDIC }; public: //--CONSTRUCTOR------------------------------------- CTimer(eTIME_TYPE _eType, DWORD _dWaitPeriod, TIMER_PROC _pFunct, LPVOID _pSegment); //--DESTRUCTOR-------------------------------------- virtual ~CTimer(); public: //--PUBLIC METHODS---------------------------------- inline const char* GetErrorMessage(){ return m_szError.c_str(); } UINT Start(); void Stop(); bool SetPeriod(DWORD _dwVal); eTIME_TYPE TimerType() { return m_eTimerType; } UINT GetTimerID(); protected: //--PROTECTED ATTRIBUTES---------------------------- TIMER_PROC m_pCALLBACKFUN; void* m_pSegm; eTIME_TYPE m_eTimerType; DWORD m_WaitPeriod; DWORD m_ThreadID; HANDLE m_hQuitEvent; HANDLE m_hThread; HANDLE m_hAdvisorThread; HANDLE m_hAdvise; HANDLE m_hQuitAdvisor; std::string m_szError; private: //--PRIVATE METHODS--------------------------------- static void TimerThread(void* _pParam); static void AdvisorThread(void* _pParam); }; The implementation //--FILE INCLUDES-------------------------------------------------------- #include <windows.h> #include "Timer.h" //!--CONSTRUCTOR----------------------------------------------------------- // // Method Name: CTimer(..., ...., ...) // /*! Notes Constructor. */ //------------------------------------------------------------------------- CTimer::CTimer(eTIME_TYPE _eType, DWORD _dWaitPeriod, TIMER_PROC _pFunct, LPVOID _pSegment) { m_pCALLBACKFUN = NULL; m_pSegm = NULL; m_eTimerType = _eType; m_WaitPeriod = _dWaitPeriod; if( m_WaitPeriod == 0 ) m_WaitPeriod = 1; m_hThread = m_hAdvisorThread = NULL; m_ThreadID=0; m_pCALLBACKFUN = _pFunct; m_pSegm = _pSegment; m_hQuitEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); m_hQuitAdvisor = ::CreateEvent(NULL, TRUE, FALSE, NULL); m_hAdvise = ::CreateEvent(NULL, FALSE, FALSE, NULL); m_hAdvisorThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AdvisorThread, this, 0, 0); } //!--DESTRUCTOR------------------------------------------------------------- // // Method Name: ~CTimer() // /*!

                                      1 Reply Last reply
                                      0
                                      • C cagespear

                                        I have a requirement in my app where I need to take some action at lets say - 24 hours after the app has opened. Clear case of using timers, but timers take input in "milisecs", is it a nice idea to give timers with very high interval like 24 hours which will come out to be - 24 * 3600 * 100 milisecs. Regards Amit

                                        D Offline
                                        D Offline
                                        David Crow
                                        wrote on last edited by
                                        #19

                                        cagespear wrote:

                                        ...I need to take some action at lets say - 24 hours after the app has opened.

                                        What if you created a scheduled task?

                                        "Love people and use things, not love things and use people." - Unknown

                                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                        1 Reply Last reply
                                        0
                                        • C CPallini

                                          Iain Clarke wrote:

                                          If you're going to cheat and read the docs and find that SetTimer can only do 2^31-1, then shame on you!

                                          Shame on me! :-D

                                          Iain Clarke wrote:

                                          I certainly wouldn't do a 24hr SetTimer - what if people chage the clock? Twice a year that gets done for you, etc...

                                          Do you think the timer would be affected (I really have no clue about but i suppose it shouldn't: it's a timer, not a clock!)?

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                          [My articles]

                                          R Offline
                                          R Offline
                                          Rajesh R Subramanian
                                          wrote on last edited by
                                          #20

                                          And here as well. :suss:

                                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                          C 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