Multiple timers on single window

This is a discussion on Multiple timers on single window within the Windows Programming forums, part of the Platform Specific Boards category; Is it possible to have more than one timer in one window/application? Want to animate different objects on different times, ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Multiple timers on single window

    Is it possible to have more than one timer in one window/application? Want to animate different objects on different times, or timings!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe that's possible, as long as they are not the same ID. But you could of course also just do something like this:
    Code:
    void handleTimer()
    {
        static int tCount = 0; 
        tCount++;
        if (tCount > 3)
        {
           doSomething;
           tCount = 0;
        }
        doEveryTick();
    }
    That will perform the "doSomething" every 5th (count 0..4) tick - and it's most likely more lightweight than having two timers running simultaneously.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    I believe that's possible, as long as they are not the same ID. But you could of course also just do something like this:
    Code:
    void handleTimer()
    {
        static int tCount = 0; 
        tCount++;
        if (tCount > 3)
        {
           doSomething;
           tCount = 0;
        }
        doEveryTick();
    }
    That will perform the "doSomething" every 5th (count 0..4) tick - and it's most likely more lightweight than having two timers running simultaneously.

    --
    Mats
    /Busy trying to understand your code/logic!!! ... ...

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by matsp View Post
    I believe that's possible, as long as they are not the same ID. But you could of course also just do something like this:
    Code:
    void handleTimer()
    {
        static int tCount = 0; 
        tCount++;
        if (tCount > 3)
        {
           doSomething;
           tCount = 0;
        }
        doEveryTick();
    }
    That will perform the "doSomething" every 5th (count 0..4) tick - and it's most likely more lightweight than having two timers running simultaneously.

    --
    Mats
    I wrote a better one long ago.
    If it's any use, for C, here it is:
    Clock
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Although it doesn't use the use the timer mechanism.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    /Busy trying to understand your code/logic!!! ... ...
    Is there any particular part of that that you find difficult? I'm happy to explain.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by BobS0327 View Post
    Will go with this, looks straight forward... Enkosi (thanx)

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by BobS0327 View Post
    Code:
    // Set two timers. 
     
    SetTimer(hwnd,             // handle to main window 
        IDT_TIMER1,            // timer identifier 
        10000,                 // 10-second interval 
        (TIMERPROC) NULL);     // no timer callback 
     
    SetTimer(hwnd,             // handle to main window 
        IDT_TIMER2,            // timer identifier 
        300000,                // five-minute interval 
        (TIMERPROC) NULL);     // no timer callback 
            
    /*
    To process the WM_TIMER messages generated by these timers, add a WM_TIMER case statement to the window procedure for the hwnd parameter.
    */
    
    case WM_TIMER: 
     
        switch (wParam) 
        { 
            case IDT_TIMER1: 
                // process the 10-second timer 
     
                 return 0; 
     
            case IDT_TIMER2: 
                // process the five-minute timer 
    
                return 0; 
        }
    Are these timers handled as threads, i mean which gets called first? Also, my idea is to simultaneously run them on different speeds (i.e. move two objects on screen on different speeds)

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    You need not worry about which gets called first. Windows handles that.
    Btw, the ID, not the handle, is sent along with the message.
    And don't cast the address of the callback function. Ever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    You need not worry about which gets called first. Windows handles that.
    Btw, the ID, not the handle, is sent along with the message.
    And don't cast the address of the callback function. Ever.
    Works.. PS: The example you see is from MSDN, i'm not casting it so its all cool

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Are these timers handled as threads, i mean which gets called first? Also, my idea is to simultaneously run them on different speeds (i.e. move two objects on screen on different speeds)
    The way a timer works is that it posts a message to your message queue with the WM_TIMER message-type, and with the relevant ID. The messages for multiple timers may be posted at once by the OS. It is then up to your message-handler (aka event-loop) to handle the WM_TIMER message - this normally means that one happens after the other, but theoretically you could come up with (complicated) code that attempts to run multiple timer event's functions at once.

    I hope this clarifies things - if not, please ask whatever it is you don't understand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by csonx_p View Post
    Works.. PS: The example you see is from MSDN, i'm not casting it so its all cool
    Alright, that's cool. Just remember that little tip.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    If either of the timers has to perform a lenghty task (ie process large data files into a DB), remember to KillTimer(), perform the task and start the timer again.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    I would also caution that as long as you are processing the WM_TIMER message, you are blocking other messages from being handled, as well.
    To this, there are two solutions.
    1) Process WM_TIMER in another thread.
    2) Keep it short.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21