Say i want to make a "loading..." appear for 5 seconds, then change to something else. How would i achieve that? Thanks
Printable View
Say i want to make a "loading..." appear for 5 seconds, then change to something else. How would i achieve that? Thanks
That'll only work under Windows. The header will vary depending on your OS, but the function name and general idea will be very similar.Code:#include <windows.h>
...
Sleep(5000) // Pause 5,000 milliseconds
For a somehwhat kludgy, yet standard way, look at the functions in the <ctime> header. They allow you to get the current time. So, if you record the starting time, then enter an infinite loop until the desired time difference is reached (by successively checking the time, perhaps not every loop iteration, though), then break out of the loop. Not terribly accurate or nice, but it should work.
Or for posix,
#include <unistd.h>
unsigned int sleep(unsigned int period);
Note that it is in seconds.
Or, for conformance to UNIX 98,
#include <unistd.h>
int usleep(useconds_t period);
That's in microseconds.
Thanks
this is the way I would do it, but that eats up your CPU cycles like you wouldn't believe... but then again it makes it look like your program is actually doing something for those 5 seconds and not just wasting everybody's time...Quote:
Originally Posted by Zach L.
[offtopic]
Did anyone else see the subject of the OP and giggle uncontrollably?
[/offtopic]Code:#include <ctimectrl>
int main()
{
jump_ahead("5:00");
return 0;
}
I was thinking more along the lines of:Quote:
Originally Posted by pianorain
Code:#include <iostream>
using std::cout;
int main()
{
cout << "The Time Machine\n";
cout << "By H.G. Wells\n\n";
cout << "Chapter 1\n\n";
cout << "The Time Traveller (for so it will be convenient to speak of him) ";
cout << "was expounding a recondite matter to us. His grey eyes shone ";
cout << "and twinkeled, and his usually pale face was flushed and animated.";
etc...
}
I was just asking myself, "How do u control time in c++? Forget C++. How do you control time in reality?"Quote:
Did anyone else see the subject of the OP and giggle uncontrollably?
time control wasn't part of the C standard. The timecntrl header wasn't implemented until the C142 standard came out, so it's only <timecntrl>Quote:
Originally Posted by pianorain
don't forget that it's implementation-specific, so you don't want to get that stuck in an infinite loop...
RIGHT !!!Quote:
but that eats up your CPU cycles like you wouldn't believe...
This is an interesting case where it’s better to go with the non-standard code! Sleep(), etc., will release your CPU to do other stuff. Like major_small said, using the ISO/ANSI time functions will hog the CPU for the sole purpose of wasting time! This is BAD thing to do with a multitasking system.
If possible, it’s better check if “loading” is actually complete than, to use a fixed 5-second delay. For example, you could use Sleep() in a loop and check it every half-second or so.
BTW- I have a BASIC :( function: DelayDot(Delay, Count) that displays “Wait.......” Count is the number of “dots”, and Delay is the time between “dots”. I’ve used it kinda-like a library function in several BASIC :( programs.
d00der... in this case it's not bad... if you want to make it look like your program's doing something, this is a good thing... and it's not the functions that are doing it, it's your semi-infinite loop that's eating the cycles... find a way to slow the loop and you've got a way to lower the amount of CPU cycles you're using up...Quote:
Originally Posted by DougDbug
Sleep() actually puts your program's thread into a sleep mode, where it can't do anything... but using the method I said, you can have your program "do that for 5 minutes"...
The Boost.Thread library provides a sleep function that works on multiple platforms.
You can update a progress bar several times a second if you wish. You don't need to use-up all of the CPU cycles to do this.Quote:
if you want to make it look like your program's doing something, this is a good thing...
Ummm... like Sleep() ? :DQuote:
find a way to slow the loop and you've got a way to lower the amount of CPU cycles you're using up...
OK, it's not such a terrible thing for a user-mode program to waste a few (or a few billion) cycles for 5 seconds once in a while. The operating system will interrupt the time-wasting loop as it allocates time to other applications & processes. And, most of the time there's nothing else for the processor to do anyway. It's just one of those "what if everybody did it?" situations... If you were running several applications, and they are all running lots of tight "do-nothing" loops, you'd notice it.
>>If you were running several applications, and they are all running lots of tight "do-nothing" loops, you'd notice it.
I notice it when I have an un-Sleep(1)'ed PeekMessage() loop running, and I'm trying to compile a second program that's supposed to interact with it :) i.e. the second program takes about 20 times as long to compile, everything else on the computer slows to a standstill including switching apps etc..
Moral of the story: Don't deadsleep your programs for extended periods of time :D