Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to wait for x seconds?

Posted on 2006-07-10
12
Medium Priority
?
2,556 Views
Last Modified: 2010-10-05
Hi!

 while(y < 2000){
                                mes->records[2].Time++;
                                randomize();
                                mes->records[2].Measure_Value = 10*sin((random(100)%10)/3);
                                i = 0;
                                while(i < 10000000) {
                                      ++i;
                                }
                                ++y;
                                VMes = static_cast<void *>(mes);
                                long int PktSize = sizeof(*mes);
                                AThread->Connection->WriteBuffer(VMes, PktSize);
}

I would like to remove the while-loop

while(i < 10000000) {
                                      ++i;
}

with a wait call or something like it that will wait for 1 second in every loop.
Any suggestions?
0
Comment
Question by:mikrodidakt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 180 total points
ID: 17072077
sleep(1);    or  Sleep( 1000 );

But why do you want to wait for one second?  

It's usually not a good thing to wait a fixed amount of time, if without the wait something breaks.  

0
 

Author Comment

by:mikrodidakt
ID: 17072274
I am simulating a application that will sen a message every 1-2 seconds.
But where can i find this sleep function when i try to use it it cant find it in wat h-file is it included in?
Is this a standard call i C++?
0
 
LVL 30

Expert Comment

by:Axter
ID: 17072497
Hi mikrodidakt,
> But where can i find this sleep function
If you're on a Windows platform, it's a windows API function that needs the windows.h header

#include <windows.h>
And on Windows, it's Sleep, and not sleep.

Cheers!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:payalpr2002
ID: 17072500
Include "windows.h" file.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17072599
> It's usually not a good thing to wait a fixed amount of time, if without the wait something breaks.

There is a lot of wisdom in that grg99ism. Take heed!
0
 

Author Comment

by:mikrodidakt
ID: 17072838
I get this error

[C++ Error] PostCodeServer.cpp(75): E2015 Ambiguity between '_fastcall Idglobal::Sleep(unsigned int)' and '__stdcall Sleep(unsigned long)'

ok if Sleep is a bad ide is there any time method that i can use so that i know when 1 second has passed it dont have to be a exact method i just want to wait for 1-3 seconds between every package that i send?
0
 

Author Comment

by:mikrodidakt
ID: 17072897
I used this
Idglobal::Sleep(unsigned int)

And i got it to work
0
 
LVL 8

Accepted Solution

by:
List244 earned 195 total points
ID: 17073152
int STime = GetTickCount();
while(GetTickCount() -STime < 3000){} //Wait 3 seconds

While the above will do the same as sleep (Pause your program) if you had some main loop or another way
to allow your program to continue running, you could avoid 'sleeping' your program.  You could do this by
using an if-statement which is often checked.  Then if the tick count has raised by 1000 a second has passed.
Then, if that has happened, you could go back to the function and send the next packet.
0
 
LVL 8

Expert Comment

by:List244
ID: 17073195
Unless that is the case though, where you can exit the function and re-enter it later to continue sending, you might
as well stick to sleep.  The only problem (I know) with using sleep is it pauses your entire program.  Where if you could
keep track of time, other events could take place during those 3 seconds, and if the time were hit then you could revisit
that action.  Which would create a smoother running application.  However, it really depends on what you are building.
For example, in a game, it would be a must, because you can not pause a live game for 3 seconds while you send a
packet.  While a simple console-application would really not benefit because while it is doing its actions the user really
has no need to contine with inputting (Normally in console applications you input expecting output, so they should be
waiting for that).

So, sleep can be useful, and can be used without trouble.  You really just need to think about what is is you need for
your program.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17073300
> GetTickCount()

You mean having this in your message processing loop? Better to have a Windows timer, if so.

Are you using a worker thread for the every-three-seconds posting? If so, ::Sleep may be fine.
0
 
LVL 8

Expert Comment

by:List244
ID: 17073482
GetTickCount can be handled many ways and while a timer may be better, it is not as easy implement.
0
 
LVL 11

Expert Comment

by:cup
ID: 17076515
You could also have used

::Sleep(3000);
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

670 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