{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: Cpp :: Pause your application

[ 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!

 Last edited 2002-12-07 18:37:06 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0