How do I do something every x increment in time?

From: wwwolf (abuse_at_alltel.net)
Date: 03/30/05


Date: Tue, 29 Mar 2005 19:38:31 -0500

How do I write code that executes a statement every 0.25 seconds while other
conditions are still being evaluated? (I hope I got the terminology right)

Psuedocode:
1. Set up a timer to display a message 5 seconds from now.
2. While 1 is activated, "cout << "." << flush;" once every 0.25 seconds until 1
is ready to print its message.

I understand I will need 2 time variables. 1 will be "timer" and 2 will be
"dotDelay". I think I will need to evaluate dotDelay and any dotDelay
statements while it is placed inside the "timer while block" since this block
needs to keep control. Or, dotDelay needs to be given control to execute its
statement every 0.25 seconds and then pass control back to the timer loop.

I've noticed that loops will 'delay' the next statement outside of their block
loop until the loop returns false. At first I tried to set up a while loop
inside of another while loop to 'delay' the "." message. It did exactly what I
told it to (not what I wanted) by delaying the "." until x time and then
continue to blast the screen with the "." message until timer was ready to
cout.

1. What is the terminology called for the block that executes a statement every
x increment in time?

2. Is this where a function would be needed?

Could someone show me an example of how to set this up?
Thanx again for your patience.

// Example for 5 second timer w/ delay dot

#include <iostream>
#include <ctime>

int main()
{
   using std::cin;
   using std::cout;
   using std::flush;

   time_t timer = ((time (0)) +5); // Add 5 sec to current time

   while( timer >= time (0) ) // Compare the timer w/ current time
      {
      cout << "." << flush; // Would like this to only execute every 0.25
seconds
      }
   cout << "\nFive seconds have passed.\n" << flush;
   return 0;
}

If I have gotten any terminology wrong then plaease correct me. One of the best
tools for learning is being able to ask the right questions.

Thanx again,



Relevant Pages

  • Re: How do I do something every x increment in time?
    ... dotDelay needs to be given control to execute ... > statement every 0.25 seconds and then pass control back to the timer loop. ... What is the terminology called for the block that executes a statement ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Polling, Interrupts, DMA, Synchronous, Asynchronous I/O Definitions
    ... the terminology is less useful than it might be. ... though a "message loop" could arguably be claimed to be ... considering in a particular iteration but what is true for _ALL_ ... watching the "polling" version eating up every single CPU cycle ...
    (alt.lang.asm)
  • Re: question about thread scheduling
    ... I will try what you suggested, the reason that I didn't use the sleep method ... whatsoever to tamper with the timer tick. ... If the NN run in a different thread as the control loop ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Timer module for interpreter
    ... What I'd like to do is have a fall-back timer so that if some ... loop which is running the code. ... Presumably the flag set by the ... module and checked by the main interpreter loop would also have ...
    (comp.sys.acorn.programmer)
  • Re: Timer module for interpreter
    ... What I'd like to do is have a fall-back timer so that if some ... loop which is running the code. ... Several of my applications use SWI OS_CallAfter to set a routine to be ... intervals to allow cooperatively multi-task to continue. ...
    (comp.sys.acorn.programmer)