All I want to do is for some block of code to run every 10 minutes or about. Can anyone tell me is there some standard or common way of implenting a block of code that just buys some time or is it a simple loop like the following:

int i;
for (i = 0; i < 100000; i++)
{
.... do nothing code
}

Justmehere,

I have used a library to implement a function like that. As a matter of fact the timer was a small part of the library, however it is still part of it.
Check out the Allegro library:

Now, I cannot say that this is the easiest nor the most efficient way to implement a timer, however it is one way.

good luck!

And a forum specifically for Allegro help: http://www.allegro.cc

If all you want to do is wait for a while you can use sleep...:

#include <windows.h>
int main() {
  // Do something
  Sleep( 1000 ); // Sleep( miliseconds );
  // Do more somethings
  return 0;
}

You can put the sleep in an infinite loop if you want it to execute every while. Alternatively you could create some kind of time comparison thing... get the current time and wait till it reaches that time plus X... I think this way's better. I've assumed you're using windows... Sleep takes a value of miliseconds. So if you want to work with seconds just do something like:

Sleep( 1 *1000 );

Which will wait one second. Change the first number to modify it.

Comments
A nice simple C++ timer, Thanks a lot!

twomers

Awesome, thanks for showing him an easier method, I am still learning myself, and I will put this post to good use!

Thanks to both.

I'm using just straight C and no MFC or Boost or Borland classes. Ok but do you know if the sleep mode is the least CPU intensive method. I just ran the for loop idea in a basic C program and my CPU useage was up to 100%. Ideally the program I am writing will run every 10 minutes or about and then do its thing. But I don't want to hog CPU resources from other 3rd party applications I have running on the same computer.

Thanks again the Sleep function works beautifully. The CPU useage is what I was hoping for.

This article has been dead for over six months. Start a new discussion instead.