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. OnTimer function is killed automatically when the dialog is closed

OnTimer function is killed automatically when the dialog is closed

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
19 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.
  • S shanmugarajaa

    Hi, how to create "TIMERPROC".. Right now my application is developed using MCF. can you provide me any example code that may help me to resolve my problem. Regrads, S.Shanmugaraja

    B Offline
    B Offline
    bjorn_ht
    wrote on last edited by
    #8

    Create a console app and put this code in to see how it works, then you can adapt "myOnTimer" and the call to ::SetTimer to your code.

    #include <windows.h>
    #include <iostream>

    void CALLBACK myOnTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD tickCount)
    {
    std::cout << "Timer. id = " << idEvent << ", tick = " << tickCount << std::endl;
    }

    int main(int argc, char * argv[])
    {
    MSG msg;

    UINT_PTR timerId = ::SetTimer(0, 42, 1000, myOnTimer);

    std::cout << "Timer set, id = " << timerId << std::endl;

    // This is done by MFC in your application but needed for the example.
    while(GetMessage(&msg, 0, 0, 0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }

    S 1 Reply Last reply
    0
    • B bjorn_ht

      Create a console app and put this code in to see how it works, then you can adapt "myOnTimer" and the call to ::SetTimer to your code.

      #include <windows.h>
      #include <iostream>

      void CALLBACK myOnTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD tickCount)
      {
      std::cout << "Timer. id = " << idEvent << ", tick = " << tickCount << std::endl;
      }

      int main(int argc, char * argv[])
      {
      MSG msg;

      UINT_PTR timerId = ::SetTimer(0, 42, 1000, myOnTimer);

      std::cout << "Timer set, id = " << timerId << std::endl;

      // This is done by MFC in your application but needed for the example.
      while(GetMessage(&msg, 0, 0, 0))
      {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
      return 0;
      }

      S Offline
      S Offline
      shanmugarajaa
      wrote on last edited by
      #9

      But I am get error when i try to implement UINT_PTR timerId = ::SetTimer(0, 42, 1000, myOnTimer); in MFC VS6 ErrorReport:

      'SetTimer' : cannot convert parameter 4 from 'void (struct HWND__ *,unsigned int,unsigned long,unsigned long)' to 'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)'

      B 1 Reply Last reply
      0
      • S shanmugarajaa

        But I am get error when i try to implement UINT_PTR timerId = ::SetTimer(0, 42, 1000, myOnTimer); in MFC VS6 ErrorReport:

        'SetTimer' : cannot convert parameter 4 from 'void (struct HWND__ *,unsigned int,unsigned long,unsigned long)' to 'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)'

        B Offline
        B Offline
        bjorn_ht
        wrote on last edited by
        #10

        I wrote that example for VS 2008, looks like the callback signature has changed a little since VS6. Try to change it to

        void __stdcall myOnTimer(HWND hwnd, unsigned int uMsg, unsigned int idEvent, DWORD tickCount)
        {
        // ...
        }

        as the error message suggests. If you have an MSDN library with your VS then you should be able to find the correct callback function signature there. Maybe even an example.

        S 2 Replies Last reply
        0
        • S shanmugarajaa

          Hi, how to create "TIMERPROC".. Right now my application is developed using MCF. can you provide me any example code that may help me to resolve my problem. Regrads, S.Shanmugaraja

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

          shanmugarajaa wrote:

          how to create "TIMERPROC"..

          It's in the docs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644901(v=vs.85).aspx#creating_timer[^]

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          1 Reply Last reply
          0
          • B bjorn_ht

            I wrote that example for VS 2008, looks like the callback signature has changed a little since VS6. Try to change it to

            void __stdcall myOnTimer(HWND hwnd, unsigned int uMsg, unsigned int idEvent, DWORD tickCount)
            {
            // ...
            }

            as the error message suggests. If you have an MSDN library with your VS then you should be able to find the correct callback function signature there. Maybe even an example.

            S Offline
            S Offline
            shanmugarajaa
            wrote on last edited by
            #12

            Thanks you so much.

            1 Reply Last reply
            0
            • B bjorn_ht

              I wrote that example for VS 2008, looks like the callback signature has changed a little since VS6. Try to change it to

              void __stdcall myOnTimer(HWND hwnd, unsigned int uMsg, unsigned int idEvent, DWORD tickCount)
              {
              // ...
              }

              as the error message suggests. If you have an MSDN library with your VS then you should be able to find the correct callback function signature there. Maybe even an example.

              S Offline
              S Offline
              shanmugarajaa
              wrote on last edited by
              #13

              Hi code working good.. but I cant able to kill this timer... can you help to kill this call.. Regards, S.Shanmugaraja

              B 1 Reply Last reply
              0
              • S shanmugarajaa

                Hi code working good.. but I cant able to kill this timer... can you help to kill this call.. Regards, S.Shanmugaraja

                B Offline
                B Offline
                bjorn_ht
                wrote on last edited by
                #14

                You just call ::KillTimer(0, timerId); where timerId is the timer id value you got back from ::SetTimer.

                S 3 Replies Last reply
                0
                • B bjorn_ht

                  You just call ::KillTimer(0, timerId); where timerId is the timer id value you got back from ::SetTimer.

                  S Offline
                  S Offline
                  shanmugarajaa
                  wrote on last edited by
                  #15

                  Thanks Thanks so much :)

                  1 Reply Last reply
                  0
                  • B bjorn_ht

                    You just call ::KillTimer(0, timerId); where timerId is the timer id value you got back from ::SetTimer.

                    S Offline
                    S Offline
                    shanmugarajaa
                    wrote on last edited by
                    #16

                    Hi, I am beginner to MFC application. I need your suggestion to improve my skill in MFC. How you come know about __stdcall and other functionality of MFC. Any material you have been using? kindly share your views and ideas. It may help me to improve my MFC skills. I am looking forward your valuable suggestion. Regards, S.Shanmugaraja

                    B 1 Reply Last reply
                    0
                    • B bjorn_ht

                      You just call ::KillTimer(0, timerId); where timerId is the timer id value you got back from ::SetTimer.

                      S Offline
                      S Offline
                      shanmugarajaa
                      wrote on last edited by
                      #17

                      Hi, I am Calling CapWatchID: = ::SetTimer(SetTimer(0, 42, 1000, CaptureStopWatchT); and after sometime I killed this running timer using ::KillTimer(0, CapWatchID);. But the problem is, when I start the same timer again then Updation time became lessthan 1000 millisecond. Why its happing like this. kindly help me to fix this problem. Regards, S.Shanmugaraja.

                      B 1 Reply Last reply
                      0
                      • S shanmugarajaa

                        Hi, I am beginner to MFC application. I need your suggestion to improve my skill in MFC. How you come know about __stdcall and other functionality of MFC. Any material you have been using? kindly share your views and ideas. It may help me to improve my MFC skills. I am looking forward your valuable suggestion. Regards, S.Shanmugaraja

                        B Offline
                        B Offline
                        bjorn_ht
                        wrote on last edited by
                        #18

                        This is not MFC as such. SetTimer and KillTimer Windows system functions documented by Microsoft. http://msdn.microsoft.com/library/default.aspx[^] There are many good books and online resources on the subject of C++ programming, Windows system programming and MFC. I suggest you try searching the net for recommendations and book reviews.

                        1 Reply Last reply
                        0
                        • S shanmugarajaa

                          Hi, I am Calling CapWatchID: = ::SetTimer(SetTimer(0, 42, 1000, CaptureStopWatchT); and after sometime I killed this running timer using ::KillTimer(0, CapWatchID);. But the problem is, when I start the same timer again then Updation time became lessthan 1000 millisecond. Why its happing like this. kindly help me to fix this problem. Regards, S.Shanmugaraja.

                          B Offline
                          B Offline
                          bjorn_ht
                          wrote on last edited by
                          #19

                          I have not observed similar behavior so I'm afraid I can't help with that. Also please note that the number 42 as second parameter to SetTimer in my example was chosen randomly. Probably, it would be more correct in your context to use the value 0.

                          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