Thread: Setting a Timer

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Setting a Timer

    Hello, I've been wondering if their was a way to put a time limit in a console app. I know that you can do it using the SetTimer() function, but that is only for windows applications. I dont want to use Sleep() because that freezes the whole program. I just want to set a time limit. Like after 5 min, exit the program, no matter what.Is this possible in a console app?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    http://msdn.microsoft.com/library/de..._functions.asp => http://msdn.microsoft.com/library/de...ttickcount.asp

    Code:
    DWORD dwStart = GetTickCount();
    
    // Stop if this has taken too long
    if( GetTickCount() - dwStart >= TIMELIMIT )
        Cancel();
    Straight from the MSDN doc's. Great place to look for Windows programming

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I wanted to set a time limit in a console app, not windows.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I assumed you were on Windows since you mentioned windows specific functions liek Sleep and SetTimer...if it was a mal-assumption, sorry. You could probably do a very similar thing with time.h's clock() function. But just for kicks, try compiling this:

    Code:
    #include <windows.h>
    #include <iostream>
    #define ever ;;
    
    
    void dostuff() { std::cout << "haw haw: << std::endl; }
    int main()
    {
    	DWORD dwStart = GetTickCount();
    
    	for (ever) // haw haw...
    	{
    		if( GetTickCount() - dwStart >= 1000 )
    			return 0;
    
    		else dostuff();
    	}
    }
    Edit: Oh right, C++, iostream, bleh.
    Last edited by Tonto; 07-27-2005 at 06:06 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Like after 5 min, exit the program
    That could definitely be done with clock(), which is ANSI standard.

    Code:
    #include <iostream>
    #include <ctime>
    
    using std::cout;
    
    int main(void) {
        time_t t;
    
        cout << "Starting . . .\n";
    
        t = clock();
        while((clock()-t)/CLOCKS_PER_SEC < 10) {  /* for 10 seconds */
            /* ... */
        }
        return 0;
    }
    To continue the while loop for 5 minutes, change the 10 to 60*5.
    Last edited by dwks; 07-27-2005 at 05:28 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Your favourite fantasy game setting?
    By fry in forum Game Programming
    Replies: 4
    Last Post: 10-16-2002, 06:26 AM