Thread: counting in milliseconds

  1. #1
    Registered User laserbeak's Avatar
    Join Date
    Jul 2006
    Posts
    5

    counting in milliseconds

    how do i count in milliseonds? i hear that windows.h allows you do count in milliseconds from the time that windows has started. but how do i count from a specified time?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    you can use GetTickCount(), SetTimer() which are accurate to around 25-50ms.

    QueryPerformanceFrequency() and QueryPerformanceCounter() are better resolution.

    But as time msgs have very low priority in the OS msg queue they can (and are ignored if you are sending lots).

    I would not expect any timer to be acurate to one millisecond.

    >>but how do i count from a specified time?

    //get current time
    DWORD dwStart=GetTickCount();

    //do stuff

    //get finish time
    DWORD dwEnd=GetTickCount();

    //work out the diff
    int iTimeInMilliSeconds=dwEnd-dwStart;
    Last edited by novacain; 07-07-2006 at 02:13 AM.
    "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

  3. #3
    Registered User laserbeak's Avatar
    Join Date
    Jul 2006
    Posts
    5
    cool thanks i'm assuming that the compiler is fast enough to work this out in realtime?
    and it's not that reliable you said? will probably counting up to 33 milliseconds to start out with.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...i'm assuming that the compiler is fast enough...
    No. The compiler has nothing to do with it. The compiler will make the same EXE file on a slow or fast computer.

    It's the multitasking operating system. The operating system decides for itself when to update the time-variables, and when your program is allowed to read these variables.

    There is a way to take control away from the operating system. A kernel mode driver can do it. But that's advanced sthuff!

    EDIT- Just to clarify something... The elapsed-time can be reported in milliseconds, but it's not accurate to the millisecond.

    Here's an analogy: Let's say you have to walk out of the room to look at the clock.... You usually check the time every 6 or 7 minutes.

    If I ask you "What time is it? You tell me "Last time I looked it was 10:27 and 32 seconds" You have given me the time down to the second, but we know it's not accurate to the second.
    Last edited by DougDbug; 07-07-2006 at 11:46 AM.

  5. #5
    Registered User laserbeak's Avatar
    Join Date
    Jul 2006
    Posts
    5
    i want it to be used for realtime music creation this kernel seems to be necessary
    thanks for the hellp i'll try to find out more

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    For music, you really should look into MIDI. Every soundcard/soundchip has a MIDI synthesizer built-in. And, the Direct-X drivers should take care of most timing issues. I'm pretty sure you can still muck it up if you're doing lots of multitasking while playing a MIDI file.

    This site has some good information about making your porgram play MIDI sounds. It's a bit outdated... It doesn't use Direct-X, but from the info on this site, I was able to write a little program that plays a few notes & chords.

    You can find documantation and examples for MIDI & Direct-X in the Direct-X SDK and in the Platform SDK

    I have a good book called Direct-X Audio Exposed, by Todd Fay. But, it appears to be out of print.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As outlined by DougDbug, threads are scheduled on a round-robin basis. Each thread gets up to 10ms or so (most threads go to sleep before their time is up) before the next thread is run. Therefore, if there are multiple threads running, getting time accuracy below 10ms will usually be impossible.

    However, Windows has a schedule priority system. Lower priority threads will not be run if there are higher priority threads waiting. This means that if you give your thread high enough priority, you can get it to run without being interrupted by other threads and therefore get good time accuracy. The trouble with this technique is that it will make the system unresponsive, so needs to be used with caution. Your thread should yield whenever possible (using Sleep(1)) so that lower priority threads can run.

    You can read up on this at MSDN: Scheduling Priorities. The functions of interest are SetPriorityClass and SetThreadPriority.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time in milliseconds
    By coder_009 in forum C Programming
    Replies: 4
    Last Post: 05-09-2008, 03:36 AM
  2. text comparing
    By Picachu in forum C Programming
    Replies: 48
    Last Post: 12-14-2006, 03:44 AM
  3. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-05-2005, 12:46 AM
  4. Time in milliseconds?
    By d00b in forum C++ Programming
    Replies: 3
    Last Post: 08-05-2002, 09:33 PM
  5. Milliseconds
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 11-01-2001, 03:13 PM