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 Timer with non-dialog class

Using Timer with non-dialog class

Scheduled Pinned Locked Moved C / C++ / MFC
question
22 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.
  • A aangerma

    Hello, I have a class that doesn't connect to a dialog or window and I need to do some operations on timer but the timers works only with dialogs , does anyone know another way I do it? thank

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Just use the win 32 set timer method: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx[^] Dont forget, MFC just wraps the Win32 API, and even then not all of it (there is a lot of rich unctionality there not available in MFC), and pretty much all MFC functions have a function of the same name in the win32API. Most of these win32 funcs take a window handle as the first param, and thats generally the only difference between them, and their MFC counterpart, since the MFC one generally calls itself. So for example the win32 MessageBox() func has a HWND as the first partam, then all the rest are the same as MFC. You can also call the win32API directly form MFC code. Just use the global namespce operator :: before each call ie: ::MessageBox(hWnd, txt, msg, MB_OK); You can get window handles for other processes/wondows easilly too. FindWindow() returns a HWND. Its params are eityher the class of the window, or its text (in the title bar). You can use this to send or post messages, such as WM_COMMAND ones to another window. Like I say, there is a lot you can do outside of MFC. (Window hooks are particularly interesting things. Very very powerfull.) :)

    ============================== Nothing to say.

    A 1 Reply Last reply
    0
    • _ _AnsHUMAN_

      You can have a thread running the Sleep API. After a specified interval of time it becomes active, performs it's task and sleep's again for the specified duration. That is somewhat similar to what a timer does.

      You talk about Being HUMAN. I have it in my name AnsHUMAN

      A Offline
      A Offline
      aangerma
      wrote on last edited by
      #4

      Thanks for the answer, But the operations that I need to perform on the timer shuld have an approach to the data members and member Functions of the class , can I do it with threads?

      A 1 Reply Last reply
      0
      • L Lost User

        Just use the win 32 set timer method: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx[^] Dont forget, MFC just wraps the Win32 API, and even then not all of it (there is a lot of rich unctionality there not available in MFC), and pretty much all MFC functions have a function of the same name in the win32API. Most of these win32 funcs take a window handle as the first param, and thats generally the only difference between them, and their MFC counterpart, since the MFC one generally calls itself. So for example the win32 MessageBox() func has a HWND as the first partam, then all the rest are the same as MFC. You can also call the win32API directly form MFC code. Just use the global namespce operator :: before each call ie: ::MessageBox(hWnd, txt, msg, MB_OK); You can get window handles for other processes/wondows easilly too. FindWindow() returns a HWND. Its params are eityher the class of the window, or its text (in the title bar). You can use this to send or post messages, such as WM_COMMAND ones to another window. Like I say, there is a lot you can do outside of MFC. (Window hooks are particularly interesting things. Very very powerfull.) :)

        ============================== Nothing to say.

        A Offline
        A Offline
        aangerma
        wrote on last edited by
        #5

        Thanks a lot, As I understand I need to use SetTimer() and to give it an handle to window but the class is not connected to a dialog so what the hWnd that I need to pass?

        L 2 Replies Last reply
        0
        • A aangerma

          Thanks a lot, As I understand I need to use SetTimer() and to give it an handle to window but the class is not connected to a dialog so what the hWnd that I need to pass?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Timers have nothing to do with dialogs, they operate on any Window. The issue of passing a handle to the window is so that the Windows framework knows where to post the WM_TIMER message that gets raised when the timer expires. If you do not have a Window to connect the timer to then SetWaitableTimer()[^] may be able to help you.

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          1 Reply Last reply
          0
          • A aangerma

            Thanks a lot, As I understand I need to use SetTimer() and to give it an handle to window but the class is not connected to a dialog so what the hWnd that I need to pass?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            aangerma wrote:

            so what the hWnd that I need to pass?

            The one to your dialog/app window that handles WM_TIMER. As suggested you can get this handle with a FindWindow() specifying the title text. Or you can provide it form the fialog itself, I forget the func, something like GetSafeHwnd() or some such, but there is one for a dialog to get its own handle. Like I say, hunt through the win32 API. You will find what you need there.

            ============================== Nothing to say.

            1 Reply Last reply
            0
            • A aangerma

              Hello, I have a class that doesn't connect to a dialog or window and I need to do some operations on timer but the timers works only with dialogs , does anyone know another way I do it? thank

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #8

              In addition to what the others have suggested, maybe message only windows[^] might help.

              1 Reply Last reply
              0
              • A aangerma

                Thanks for the answer, But the operations that I need to perform on the timer shuld have an approach to the data members and member Functions of the class , can I do it with threads?

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #9

                No, not thread safe at all.

                A 1 Reply Last reply
                0
                • A Albert Holguin

                  No, not thread safe at all.

                  A Offline
                  A Offline
                  Andrew Brock
                  wrote on last edited by
                  #10

                  Could make it threadsafe with Mutexes[^]

                  A 1 Reply Last reply
                  0
                  • A aangerma

                    Hello, I have a class that doesn't connect to a dialog or window and I need to do some operations on timer but the timers works only with dialogs , does anyone know another way I do it? thank

                    P Offline
                    P Offline
                    PJ Arends
                    wrote on last edited by
                    #11

                    SetTimer[^] has a fourth parameter, an application defined callback function. Use that and set the HWND to NULL.

                    Independent ACN Business Owner

                    • Check out the possibilities for your future!
                    • Financial independance
                    • Full time or Part time
                    • In more than 20 countries through North America, Europe, Asia and the Pacific
                    • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                    Within you lies the power for good - Use it!

                    L 3 Replies Last reply
                    0
                    • P PJ Arends

                      SetTimer[^] has a fourth parameter, an application defined callback function. Use that and set the HWND to NULL.

                      Independent ACN Business Owner

                      • Check out the possibilities for your future!
                      • Financial independance
                      • Full time or Part time
                      • In more than 20 countries through North America, Europe, Asia and the Pacific
                      • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                      Within you lies the power for good - Use it!

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      He might find that difficult to specify a class member as a call back function. But yes, it is doable (and one of the reasons I prefer C, OK, you need to be a good programmer, but you can do what you want) :)

                      ============================== Nothing to say.

                      1 Reply Last reply
                      0
                      • A Andrew Brock

                        Could make it threadsafe with Mutexes[^]

                        A Offline
                        A Offline
                        Albert Holguin
                        wrote on last edited by
                        #13

                        Yeah but consider what he wants to do... if he's already using the WinAPI, might as well use the Win timers. Otherwise we'll have to answer a bunch of questions regarding mutexes next [joke ;P ].

                        1 Reply Last reply
                        0
                        • P PJ Arends

                          SetTimer[^] has a fourth parameter, an application defined callback function. Use that and set the HWND to NULL.

                          Independent ACN Business Owner

                          • Check out the possibilities for your future!
                          • Financial independance
                          • Full time or Part time
                          • In more than 20 countries through North America, Europe, Asia and the Pacific
                          • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                          Within you lies the power for good - Use it!

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          Now why didn't I think of that; good answer.

                          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                          1 Reply Last reply
                          0
                          • P PJ Arends

                            SetTimer[^] has a fourth parameter, an application defined callback function. Use that and set the HWND to NULL.

                            Independent ACN Business Owner

                            • Check out the possibilities for your future!
                            • Financial independance
                            • Full time or Part time
                            • In more than 20 countries through North America, Europe, Asia and the Pacific
                            • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                            Within you lies the power for good - Use it!

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #15

                            On reflection I decided to test this and it does not work without any windows, as there is nothing to process the WM_TIMER message, which is used by the default handler to invoke the callback routine.

                            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                            P 2 Replies Last reply
                            0
                            • L Lost User

                              On reflection I decided to test this and it does not work without any windows, as there is nothing to process the WM_TIMER message, which is used by the default handler to invoke the callback routine.

                              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                              P Offline
                              P Offline
                              PJ Arends
                              wrote on last edited by
                              #16

                              I would be interested to see your test code. I have no problems using a timer callback procedure.

                              Independent ACN Business Owner

                              • Check out the possibilities for your future!
                              • Financial independance
                              • Full time or Part time
                              • In more than 20 countries through North America, Europe, Asia and the Pacific
                              • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                              Within you lies the power for good - Use it!

                              1 Reply Last reply
                              0
                              • L Lost User

                                On reflection I decided to test this and it does not work without any windows, as there is nothing to process the WM_TIMER message, which is used by the default handler to invoke the callback routine.

                                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                                P Offline
                                P Offline
                                PJ Arends
                                wrote on last edited by
                                #17

                                Looking at it further, the app does not need a window, but if there is no window then the thread needs a message loop.

                                Independent ACN Business Owner

                                • Check out the possibilities for your future!
                                • Financial independance
                                • Full time or Part time
                                • In more than 20 countries through North America, Europe, Asia and the Pacific
                                • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                                Within you lies the power for good - Use it!

                                L 1 Reply Last reply
                                0
                                • P PJ Arends

                                  Looking at it further, the app does not need a window, but if there is no window then the thread needs a message loop.

                                  Independent ACN Business Owner

                                  • Check out the possibilities for your future!
                                  • Financial independance
                                  • Full time or Part time
                                  • In more than 20 countries through North America, Europe, Asia and the Pacific
                                  • Featuring the ACN IRIS 5000 video phone. See the person you are talking to.

                                  Within you lies the power for good - Use it!

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #18

                                  PJ Arends wrote:

                                  the thread needs a message loop.

                                  That's the issue; even with a callback the timer signals by posting a WM_TIMER message.

                                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                                  1 Reply Last reply
                                  0
                                  • A aangerma

                                    Hello, I have a class that doesn't connect to a dialog or window and I need to do some operations on timer but the timers works only with dialogs , does anyone know another way I do it? thank

                                    C Offline
                                    C Offline
                                    Chuck OToole
                                    wrote on last edited by
                                    #19

                                    I have a few apps like that (mostly system services) so at the end of "main()" (or whatever you use), stick code like this:

                                     /\* Fall into the standard message processing loop \*/
                                     
                                     static MSG msg;
                                     while (GetMessage(&msg, NULL, 0, 0))		/\* wait for incoming message \*/
                                     {
                                    	 TranslateMessage(&msg);		/\* translate it \*/
                                    	 DispatchMessage(&msg);			/\* and let windows dispatch it to WinProc \*/
                                     }
                                     return(msg.wParam);				/\* done after "quit" message processed \*/
                                    

                                    It doesn't hurt to sit in this loop while the other threads do their job and it allows them to post WM_CLOSE messages to run-down the process if necessary. Other standard messaging things works, including timers.

                                    A 1 Reply Last reply
                                    0
                                    • C Chuck OToole

                                      I have a few apps like that (mostly system services) so at the end of "main()" (or whatever you use), stick code like this:

                                       /\* Fall into the standard message processing loop \*/
                                       
                                       static MSG msg;
                                       while (GetMessage(&msg, NULL, 0, 0))		/\* wait for incoming message \*/
                                       {
                                      	 TranslateMessage(&msg);		/\* translate it \*/
                                      	 DispatchMessage(&msg);			/\* and let windows dispatch it to WinProc \*/
                                       }
                                       return(msg.wParam);				/\* done after "quit" message processed \*/
                                      

                                      It doesn't hurt to sit in this loop while the other threads do their job and it allows them to post WM_CLOSE messages to run-down the process if necessary. Other standard messaging things works, including timers.

                                      A Offline
                                      A Offline
                                      aangerma
                                      wrote on last edited by
                                      #20

                                      Thanks for the reply, can you give me more details how to use it? How can I know which message I Received by msg?

                                      C 1 Reply Last reply
                                      0
                                      • A aangerma

                                        Thanks for the reply, can you give me more details how to use it? How can I know which message I Received by msg?

                                        C Offline
                                        C Offline
                                        Chuck OToole
                                        wrote on last edited by
                                        #21

                                        This is the standard windows message pump, it only causes windows messages to get processed for apps that are not "dialog based". You don't modify this code in any way nor look at the message in that loop. The "dispatch" is done by windows to the proper messsage handler. If you have dialogs, windows does this for you.

                                        A 1 Reply Last reply
                                        0
                                        • C Chuck OToole

                                          This is the standard windows message pump, it only causes windows messages to get processed for apps that are not "dialog based". You don't modify this code in any way nor look at the message in that loop. The "dispatch" is done by windows to the proper messsage handler. If you have dialogs, windows does this for you.

                                          A Offline
                                          A Offline
                                          aangerma
                                          wrote on last edited by
                                          #22

                                          Can you give me exmple how to write a messsage handler , I did it only with the class wizard of mfc, and this is the first time I need to add a timer to a class that don't connected to any window, even if I creat a fictive window like dialog box the SetTimer(...) works only if the dialog is opened by DoModal. thanks

                                          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