Sleep Function

Posted on 2003-03-31
Medium Priority
Last Modified: 2010-04-01
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
Question by:Kev_Mc01
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
LVL 12

Expert Comment

ID: 8237571
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.


Expert Comment

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

Author Comment

ID: 8241006

Im using Microsoft Visual C++ Studio 6.0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

skyDaemon earned 100 total points
ID: 8241425
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....

LVL 12

Expert Comment

ID: 8244306
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.


Expert Comment

ID: 9566515
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

Author Comment

ID: 9569506
Thanks for all the help with the code, the assignment was graded a Merit so im happy :D

Thanks Again


Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
Suggested Courses

770 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