Thread: Timed loop in C program

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Timed loop in C program

    Hi,

    I am writing a program that must have some timed loops in it. Basically I wish to get my program to loop, outputting a value, until a certain time, then break. I know how to use a for loop to count up to a certain number and loop a certain number of times but could not find anything on how to get it to loop for say 2s or for example 1ms..

    Any ideas?

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Using a for-loop to "delay" a set amount of time is VERY BAD. It MAY be the only solution that works in some cases, but most of the time, there are better solutions.

    Unfortunately, there isn't one solution that fits everyone. So you need to tell us which OS and which compiler you are using.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    I'm using Windows XP and Microsoft Visual C++ 6.0's inbuilt compiler.

    The only thing that occurred to me was a for-loop, what would be the other possibilities of implementing a time delay?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In windows, you have Sleep() that causes the program to "sleep" for the given amount of milliseconds.

    So something like this:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
       for(int i = 0; i < 10; i++) {
         printf("%d\n", i);
         Sleep(i * 1000);
    }

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Thumbs up

    Thanks for that link, I was searching for "timing loop" etc and it didn't throw that up. I will have a good read of it.

    Thanks for the example also, I will give the Sleep() a try out in my program.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note also that the "portable" function described in the FAQ linked above are CPU-users - the loop does not put the process to sleep, it's spinning round using 100% of the CPU-time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just check the time every pass through the loop. When 2 seconds (or whatever) have gone by, stop.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Note also that the "portable" function described in the FAQ linked above are CPU-users - the loop does not put the process to sleep, it's spinning round using 100% of the CPU-time.

    --
    Mats
    The goal seems to be to output a value in a loop, continuing to loop for some set amount of time. So the purpose isn't to wait, it's to do some task for a certain duration.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    The goal seems to be to output a value in a loop, continuing to loop for some set amount of time. So the purpose isn't to wait, it's to do some task for a certain duration.
    Yes, when I saw your comment, I re-read the original post and came to that conclusion too. Which of course means that the FAQ is much more relevant.

    Just take one of the functions in the FAQ and modify it to output the value during the loop, essentially.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Quote Originally Posted by brewbuck View Post
    The goal seems to be to output a value in a loop, continuing to loop for some set amount of time. So the purpose isn't to wait, it's to do some task for a certain duration.
    Exactly, I tried the sleep function now and realised it simply waits not outputs as it waits.

    The last one in the FAQ link does the job though.

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program skipping while loop
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 03:30 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM