Solved

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

Posted on 2007-11-27
9
906 Views
Last Modified: 2008-02-01
in some scripts it's



sleep (5000 miliseconds)


what is it in c++?
0
Comment
Question by:Troudeloup
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20358917
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20358925
BTW, '500' is used because the function takes miliseconds - 5000ms are 5s.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 20358930
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Expert Comment

by:kevin_uk05
ID: 20358941
Try

void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20358968
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
 

Author Comment

by:Troudeloup
ID: 20358973
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
 

Author Comment

by:Troudeloup
ID: 20358977
yeah i am, for now.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20359061
>> Sleep(25000);

that halts for 25 seconds (25,000 miliseconds)
0
 

Author Comment

by:Troudeloup
ID: 20359105
but it doesn't


note that i use it in the context of console programming
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question