Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

578 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