Solved

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

Posted on 2007-11-27
9
899 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:Kdo
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
 
LVL 10

Expert Comment

by:kevin_uk05
ID: 20358941
Try

void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now