Sleep Function

Im creating a program based on a phonebox in c++.  The program allows the user to select between local or long distance or international calls.  It then asks the user to select the type of coins they are entering, i.e. 10 p, then how many coins they are entering, i.e. 5. A calculation will be performed to give the number of seconds the user has of call time (10*5).

Im wondering how to use a sleep function, or if there is any other way to make the seconds count down, to simulate a call, but also place on the screen approxx every 10 seconds, how long the user has left of call time.

Many Thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There are many ways to do what you want, it all depends on how your program works.

If you run in a windowing environment (windows program) you most likely do NOT want to sleep. Sleep will cause your process to be made unavailable for the time you sleep. During this time the user won't be able to resize windows, click on any buttons or anything.

In a non-windowing environment you can sleep but beware that while you sleep the user cannot do much with your program. Your program will be unresponsive while you sleep.

To simply sleep a few seconds or whatever you can use a function called 'sleep()' or some such. It is available on most systems, windows has a function called Sleep() and unix has a function called 'sleep()' which is also available to Win32 style console programs.

If you want your program to be active while you "sleep" you don't really want to sleep, you just want certain functions to be disabled while some process is taking place. For example while you have your call you probably don't want to allow the user to attempt to make another call so you simply disable the functions that is used to make a call. This is then best done by a simple timer, a timer is a function that is such that "do XXXX after YYYY timeunits". Typically XXXX is some function that will re-enable the functionality you want to disable while the call is active so that the user can once again make another phone call.

This is typically implemented in various ways depending on platform. We therefore have to know what platform you're on before we can give you the gory details of this. It is also very likely that you will have more luck if you post in a forum for your specific platform.

Yes, this is a so-called "platform question" and not a "C++ question" even if you ask for a function in C++. The C function that is available to your platform is typically the same function that C++ programs will use, however which function this is will be different on a windows platform than on a unix platform etc. So you're asking this question to the wrong forum.

Let us know which platform and which compiler you are using, and you'll get a more specific answer.
Kev_Mc01Author Commented:

Im using Microsoft Visual C++ Studio 6.0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Try these func:

//start a timer with interval 1 second (1000 milliseconds) and event id 10
long m_nTimer;
m_nTimer = SetTimer(10,1000,0);

//stop a timer

//do stuff when the timer fires
void MyClass::OnTimer(UINT nIDEvent)
// update the screen every second showing a count down timer or whatever

In the class wizard you can set the ontimer up by selecting myclass and then creating a function OnTimer for the WM_TIMER event.

This is more of an MFC way really... but you do have VC++.  There is an equivalent for straight c++ if you need....

Altervatively you could just use sleep(1000) to delay one second before updating your screen or whatever.

for(long i=0; i<nNumSeconds; i++)
  //update screen

This will lock up your program while you sleep unless you put it in a separate thread.  S'not that hard really....


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Are you running a windows program or a console program?

As you perhaps ought to understand by now, these two provide very different environments and therefore also very different ways to solve your problem.

In a windowing environment you do not want to sleep but rather want to simply disable some functionality for the time it takes to wait for the event and so you want to use a Timer and not sleep at all. Sleeping in a windows program is a big mistake it will make the program unresponsive and the user will think the program hangs.

There is a function to sleep in windows also but you shouldn't use it.

In a console program it is a different matter, there you might want to sleep for a bit so that any input the user type won't be responded to immmediately.

I therefore suggest you also tell us if you are making a windows program or a console program.

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: skyDaemon

Please leave any comments here within the next seven days.


EE Cleanup Volunteer
Kev_Mc01Author Commented:
Thanks for all the help with the code, the assignment was graded a Merit so im happy :D

Thanks Again

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.