Timer in Win32

This is a discussion on Timer in Win32 within the Windows Programming forums, part of the Platform Specific Boards category; Hello, there. I am building an application (a board game, like chess) using the Win32 API and have some problems ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    Timer in Win32

    Hello, there. I am building an application (a board game, like chess) using the Win32 API and have some problems using the timer.

    The problem is that, as I read, timer messages are not asynchronous, so if I specify 1000msec in the SetTimer call, the program is not guaranteed to receive a WM_TIMER message every second.

    I use many Sleep() calls in my program so the timer does not work very well.

    I want to use the timer to calculate the time that the PC takes to make a move.

    Anyway, is there way to send messages to the WM_TIMER every second and not depend on what the program is doing?

    Thanks..

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you really shouldn't use sleep() at all inside the same thread as a windows message loop. If you must use sleep(), may I suggest spawning off some other thread to do that action in. Basically, the message loop in the main thread is held up when you sleep and as a result, can't get the WM_TIMER message that is waiting in the queue
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >I want to use the timer to calculate the time that the PC takes to make a move.

    I don't understand why you need a timer?

    Why not take the time from the system just before the application begins making a move, again when it has a finished, and subtract the difference.

    Alternatively, if you know that you need a timer solution, use system time calls in your timer event code rather than relying on the assumption that total time is n * timer internal.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    > Why not take the time from the system just before the application begins making a move, again when it has a finished, and subtract the difference.

    Good idea, but still the need the timer, because the PC has to make the move in a certain time (added by the user) and if It doesn´t loses.

    >use system time calls in your timer event code rather than relying on the assumption that total time is n * timer internal.

    What do you mean by that?

    Thanks.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by nicolas
    > Why not take the time from the system just before the application begins making a move, again when it has a finished, and subtract the difference.

    Good idea, but still the need the timer, because the PC has to make the move in a certain time (added by the user) and if It doesn´t loses.

    >use system time calls in your timer event code rather than relying on the assumption that total time is n * timer internal.

    What do you mean by that?

    Thanks.
    One solution you could try is spawning a thread when the computer begins to calculate it's move. Inside this thread you could just keep track of the elapsed time. If it is ever greater than the user specified amount, then it can send a message via a callback. If the computer calculates a move first, it can simply destroy the thread.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    Perhaps this example helps you to get rid of WM_TIMER:

    // time measurement

    #include <iostream>
    #include <ctime> // clock_t, clock(), ...
    #include <conio.h>
    #include <cmath>
    using namespace std;

    int main()
    {
    const int NMAX = 20;
    int max;
    cout << "number of loops? ";
    cin >> max;
    clock_t t1,t2;
    double ts, tm=0;

    double a;

    cout << endl;
    for(int n=0; n<NMAX; ++n)
    {
    t1 = clock(); // start
    for( int i=0; i<max;i++)
    {
    a = sin(a); //action 1
    }
    t2 = clock(); // end
    ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); // time in seconds.
    cout << "time action 1: " << ts << " sec" << endl;
    tm += ts;
    }
    tm/=NMAX;
    cout << "average time action 1: " << tm << " sec" << endl;

    tm=0;
    cout << endl;
    for(int n=0; n<NMAX; ++n)
    {
    t1 = clock();
    for( int i=0; i<max;i++)
    {
    a = cos(a); //action 2
    }
    t2 = clock();
    ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC);
    cout << "time action 2: " << ts << " sec" << endl;
    tm += ts;
    }
    tm/=NMAX;
    cout << "average time action 2: " << tm << " sec" << endl;

    getch();
    }

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. win32 timer function
    By Anuradh_a in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2008, 10:51 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM

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