Possible to run while Loop Every 5 Seconds?

This is a discussion on Possible to run while Loop Every 5 Seconds? within the C++ Programming forums, part of the General Programming Boards category; Code: while(true) { if (system uptime % 5 == 0) { //do stuff } } Is there a better way ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Possible to run while Loop Every 5 Seconds?

    Code:
    while(true)
    {
    	if (system uptime % 5 == 0)
    	{
    		//do stuff
    	}
    }
    Is there a better way to do this than with the system up time? (Is there an actual command in C++ to perform an action every x seconds?)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Most OS's have a sleep command (with various capitalizations) that allow your program to cede x amount of time to other processes and resume afterwards.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by tabstop View Post
    Most OS's have a sleep command (with various capitalizations) that allow your program to cede x amount of time to other processes and resume afterwards.
    Thanks!

    Code:
    int x = 0;
    while(true)
    {
    	cout << "Hello World! " << x << "\n";
    	x++;
    	sleep(5);
    }
    Works great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 06:47 PM
  2. Program will not run thorugh the loop
    By Babs21 in forum C Programming
    Replies: 6
    Last Post: 11-10-2007, 12:59 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. loop to store values
    By keendoc in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2005, 03:43 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM

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