Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

sleep (sec 5 ) in a generic c++ code and a win32 code.

in some scripts it's



sleep (5000 miliseconds)


what is it in c++?
Avatar of jkr
jkr
Flag of Germany image

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Troudeloup,

That depends on the C++ implementation.

If the library supports it, the easiest is probably to instantiate a TTimer, set the interval for 5000 ms, and let the OnTimer event handle it.  That's not a true sleep(), but allows the application to continue and to fire off a method at the 5 second mark.


Good Luck,
Kent
Try

void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
I think Troudeloup is working for Windows, so the Sleep() function should work.
Another portable alternative, not exact up to the millisecond, could be:

time_t t0 = time(NULL) + 5;  // 5 seconds
while (time(NULL) < t0);
Avatar of Troudeloup
Troudeloup

ASKER

jkr, this doesn't halt for 5 seconds.


#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;




int main ()
{

      Sleep(25000);

      cout << "what's going n ?" << endl;

      
      
      
      return 0;
}



yeah i am, for now.

>> Sleep(25000);

that halts for 25 seconds (25,000 miliseconds)
but it doesn't


note that i use it in the context of console programming