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++?
TroudeloupAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
BTW, '500' is used because the function takes miliseconds - 5000ms are 5s.
0
 
jkrCommented:
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kevin_uk05Commented:
Try

void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
0
 
Jaime OlivaresSoftware ArchitectCommented:
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);
0
 
TroudeloupAuthor Commented:
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;
}



0
 
TroudeloupAuthor Commented:
yeah i am, for now.

0
 
Jaime OlivaresSoftware ArchitectCommented:
>> Sleep(25000);

that halts for 25 seconds (25,000 miliseconds)
0
 
TroudeloupAuthor Commented:
but it doesn't


note that i use it in the context of console programming
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.