Thread: Timer issue

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    132

    Timer issue

    Okay I need to figure out a way to setup a timer that will up variables every second. Now I know there is a windows function that I could use (through the windows procedure functions) that will send a message of my choosing on the ineterval of my choice. Now I can't remember the function off hand, but I could find that out on my own. However, I've read that if the system is slowed down and these calls stack up then only one call would actually get through to the windows procedure function. Is there a way to setup such a timer to do a similar job (on multiple windows in an MDI interface) that would actually do the job on each interval without the possibility of loosing some of these messages (if the system slows down)?

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Use SetTimer() for the WinProc method you described. I don't think you will actually "loose" timer events (unless the message Q overflows). However, if you have a timer setup for every second, and the message loop thread gets tied up doing some work, then the timer events can get backed up and come in one after another (ie. not 1 second apart).

    Any other approach is going to involve creating another thread. It can be as simple as calling Sleep(1000) in a loop, or you could use a waitable timer.

    What do you need to do every second? Using SetTimer() may be sufficient for your purposes as long as the period between each WM_TIMER message doesn't need to be exactly 1 second.

    gg

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    132
    Yeah I guess the SetTimer() function would work (just read up on it in the MSDN), although I was thinking of another function that you could specify which windows message to pass on (thus you could setup your own windows message and pass that on after the elapsed time ended).

    In the one I was thinking of stated that if I passed on a message in short loops (say trying to keep track of time as it elapsed... which this would be a poor method for that anyways), the messages would instead of stacking in the queue would be destroyed if one in the queue already existed.

    What I'm doing is keeping track of idle time on a connection (and disconnecting the connection if the idle time reached the max). Thus every second I need the idle variable for that connection needs to be upped by 1. (And if they stack then once the system comes back up to speed the idle time would be adjusted as appropriate. However, possibly giving the user a slightly higher idle time then usually allowed, but I can live with that... if the system slows down.)

    Thanks for your help

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yep, this is common task. Have you considered keeping a "timestamp" of the last time there was activitey on the connection.
    Since detecting an expired idle connection isn't really a time critical task (I'm assuming) then you could setup a timer for 10 or 15 seconds (or whatever granularity you like) to check the deltas of the current time and the last activity "timestamp" on your connections.
    I usually use a DWORD for this, coupled with GetTickCount() (don't forget to account for wrap-around of the DWORD.)

    gg

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    132
    Okay I have it all working, now I have one question which MSDN doesn't answer about the SetTimer() function. Does this function only wait until the timer has elapsed once (or the time-out value ends once) or does it continue to do the same thing every second until you call KillTimer()? Just wondering if I have to recall the SetTimer function everytime the elapsed time ends.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It's continuous.

    gg

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    132
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM