SteinSOFT.net
  Pause your application

In this code snippet I will show you how to pause your application using the function from the previous snippet. This function is smiliar to the function Sleep() from the Win32 API but it is platform independant. Here is the code:

#include <time.h>
 
float getElapsedTimeInMs()
{
   //Return clock() as ms
   //1 Second = 1000 milliseconds
   return clock()/(CLOCKS_PER_SEC/1000);
}
 
void sleep(int ms)
{
   float goal;
 
   goal = ms + getElapsedTimeInMs();
   //Do nothing until goal is greater than the
   //current elapsed time
   while( goal >= getElapsedTimeInMs())
     ;
}

Very simple, isn't it? I recommend using this function in textconsole based applications that should be platform independant so that they can be compiled both on Windows and Linux without changing the code. Here is a little example that shows you how to use it:

cout << "I will now wait half a second..." << endl;
sleep(500); // 1/2 second = 500 ms
cout << "Half a second elapsed." << endl;

That's it! If you have questions, mail me!

Copyright © by SteinSOFT