Hold the program for x seconds
Hi,
how can I pause my program for x seconds? Building a loop with a million iterations and test if there are enough iterations would only be the last resort.
The reason is that I execute via system() an external command which takes about 0.5 seconds to finish. And only after that is finished I can proceed in my program.
Cheers,
J.
[364 byte] By [
joebar] at [2007-11-20 0:59:53]

# 1 Re: Hold the program for x seconds
Depending on what kind of system you are on (Windows, Unix, etc.), try looking for a sleep(), wait(), pause(), or setitimer() function in your 'help' or 'man' pages.
sszd at 2007-11-9 1:07:03 >

# 2 Re: Hold the program for x seconds
It's usually not a good idea to freeze the program for a long period of time.
This may confuse the user who might think the program has locked-up.
# 3 Re: Hold the program for x seconds
thanks will look into the help pages of the functions. I have a Linux system.
I agree to the freeze problem, but don't see a solution. Anyway I think for me it should be fine since
- It will freeze about 1 second
- After the user has started the progam no interaction is necessary
I am calling via system() a web browser and need the cookie I get. I've measured that it takes about 0.5 - 0.6 sec until I have the cookie.
Anyway open for other proposals.
Thanks,
J.
# 4 Re: Hold the program for x seconds
It's usually not a good idea to freeze the program for a long period of time.
This may confuse the user who might think the program has locked-up.
That depends a lot - often you will pause one thread that does regular polling. I have threads that do that. sleep() requires whole numbers of seconds but there's a workaround using select() that works in milliseconds. (On POSIX anyway). Windows has timers that work in milliseconds too if you require that there.
You don't want to iterate millions of times because that uses up CPU cycles.
What is it you are pausing for anyway? Perhaps you need some IPC.
# 5 Re: Hold the program for x seconds
What is IPC? Sorry. See above what it is for. Need to wait until firefox has retrieved a cookie.
I did the following
#include<iostream>
#include<string>
#include<unistd.h>
using namespace std;
int main (){
cout << " waiting for sleep ";
sleep(5);
cout << " finished waiting ";
return 0;
}
The result is VERY odd to me:
The program waits, then it output both cout commands. I expected the first cout command than waiting for 5 seconds and then the second cout. Whats wrong?
The system is Linux.
Thanks.
J.
# 6 Re: Hold the program for x seconds
The input and ouput streams are usually buffered for better performance. The display is updated only if the buffer is full/flushed. In MSVC sending endl to cout flushes the buffer. But i dont know about Linux try cout.flush().
I recommend use of codetags for better readability.
# 7 Re: Hold the program for x seconds
Also I would recommend IPC ( Inter Process Communication ) to do this job. Firefox is open-source. So, just check out if there is any privileges for doing such jobs. Else add your own IPC for firefox and use the service. For quick fix do a sleep().
# 8 Re: Hold the program for x seconds
flushing helps. Thanks!
What are code tags?
Yes firefox is open source, but for a beginner it would be too much to read its source code. I would have to post millions of questions :-(
Thanks for the help, it is really appriciated!
# 9 Re: Hold the program for x seconds
You are welcome.
What are code tags? See this link ( http://www.dev-archive.com/forum/misc.php?do=bbcode#code)