Thread: Timer problem

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Timer problem

    First off I want to state that I have only been doing Win32 programming for about a month now so go easy on me

    Lately I have written a few screensaver-esque programs, like bouncing lines and such. In order to get the speed correct in between drawing new lines, I have done two different things. First off I used the Sleep() function and had it sleep for about a millisecond or so before continuing on. This worked great except that I soon found out that the amount of time waiting seems to be dependant on processor speed. At home sleeping for 5 milliseconds zips along on my 1.8 GHz while at work it moves pretty slowly when sleeping at only 1 millisecond on a 900 MHz. If I take away the sleep function it moves at unrecognizable speeds so I know that my work comp can compute everything quick enough. It looks great at home but looks terrible at work since it moves too slowly.

    So then I learned about the SetTimer() function and WM_TIMER. Now the speed is the same on every computer BUT moves kinda slow on ALL comps even if I set the timer to only 1 millisecond. It won't let me input fractions of a millisecond and I even tested it on zero milliseconds - still ran as though it were at 1 millisecond.

    Is there anyway to set a timer to run less than one millisecond or another method I could try that would run the same speed on all comps?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Are you sure it is the timers that are causing you trouble?

    Consider, you call Sleep(), it wakes up and you spend n milliseconds drawing your page on a 1.8GHz machine and n+something on a 900MHz, before you call Sleep() again. Even if Sleep() is waking you exactly on time, you will not get the same performance on machines with vastly different base performance.

    The same is basically true of WM_TIMER messages. When the timer expires, a WM_TIMER message is added to your Windows input queue. If you are processing other messages in the same app, then it will take n milliseconds on a 1.8 and n+something on a 900. What you might see is uneveness, i.e. some times the WM_TIMERS come in regulaly, other times they seem to accumulate then 3-4 come along one after another.

    Also consider the possibility that other programs are running on your system, and if Windows is having to do a lot of work timeslicing between processes, individual process performance can suffer. You can alter your priority to alleviate this, but that is rarely a good idea unless you really need too.

    Try timing your routines and seeing if it is they that are the bottlenecks. A couple of good routines to have a look at are QueryPerformanceCounter() and QueryPerformanceFrequency().
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Well the reason I thought it was the Sleep timer is because if you set the timer to zero then the program zips through drawing the bouncing lines at super-sonic speeds, even on the slower computer. So the program is certainly capable of drawing at faster speeds than if I add a Sleep(1) or a SetTimer of length 1. I know that obviously the slower comp will be slower drawing, but what I want is some way to pause the program for a unit of time smaller than a millisecond but larger than zero.
    Try timing your routines and seeing if it is they that are the bottlenecks. A couple of good routines to have a look at are QueryPerformanceCounter() and QueryPerformanceFrequency().
    I'll have to look into these routines, but since I'm just starting at windows programming it might be awhile before I can get them working properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. design problem
    By hannibar in forum C Programming
    Replies: 6
    Last Post: 11-17-2005, 06:22 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. timer problem
    By robin171 in forum C Programming
    Replies: 2
    Last Post: 10-17-2005, 08:48 AM
  5. Help needed with external call, WinExec() and timer.
    By algorithm in forum Windows Programming
    Replies: 9
    Last Post: 11-07-2001, 09:35 AM