Click to See Complete Forum and Search --> : How to implement a delay???
C++ Newbie
November 29th, 2002, 04:00 PM
Hello, can someone tell me how to implement a wait function for a particular period of time (e.g., 5 secs)?
In the program I'm writing, I'm controlling a piece of equipment that has a reset command which must be followed by a run command after the equipment has rebooted. Consequently, I must do the following:
{
//Reset the module
api_reset_module();
//Need to wait for 5 secs
for (i=0;i<20000;i++)
dummyvariable++;
//Now restart the module
api_restart_module();
}
Currently, I just have a for loop stuck in there at a loop count that effectively gets me ~> 5 sec delay but this is not very elegant I know!
Can someone tell me if there is a way to implement a specific delay in a more elegant way?
Thanks
The Newb
Andreas Masur
November 29th, 2002, 04:11 PM
There are several options. You can use 'Sleep(5000)' which will stoip execution of this thread for 5 seconds. The downside of doing this is for example if you have a GUI application your message queue will also not be processed during this time. So in other words there is no redrawing or other action (e.g. pressing a button).
Another possibility is using other timers like waitable timers. The following is a sample how to use waitable timers. They will delay execution by 5 seconds in the following...
#include <iostream>
#include <windows.h>
#include <conio.h>
int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER liTime;
liTime.QuadPart=-50000000;
// Create a waitable timer.
hTimer = ::CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if(!hTimer)
{
std::cout << "Could not create waitable timer (Error "
<< ::GetLastError() << ")" << std::endl;
::Sleep(5000);
return -1;
}
std::cout << "Wait for 10 seconds..." << std::endl;
// Set timer
if(!::SetWaitableTimer(hTimer, &liTime, 0, NULL, NULL, 0))
{
std::cout << "Could not set waitable timer (Error "
<< ::GetLastError() << std::endl;
::Sleep(5000);
return -1;
}
// Wait for timer
if(::WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
std::cout << "Could not wait for timer object (Error "
<< ::GetLastError() << std::endl;
else
std::cout << "Time elapsed" << std::endl;
// Wait for keystroke
_getch();
return 0;
}
For additional information you might take a look at http://www.codeproject.com/system/timers_intro.asp. It is a very comprehensive timer tutorial.
KevinHall
November 29th, 2002, 09:48 PM
Sleep() works great if you are using Windows. If you are using an OS with POSIX support, then use nanosleep(). You can usually find an example in the man pages.
- Kevin
PaulWendt
November 29th, 2002, 10:41 PM
These guys made only correct statements. The only thing that I
want to add is that you have to look in the OS documentation.
This sort of thing is almost always going to be platform-specific
because of the different hardware involved. If you're in neither
windows nor a unix based OS [like if this is an embedded systems
program], you'll have to turn to the OS manual then.
Chances are, though, that Sleep(), sleep(), or nanosleep will help
you just fine.
--Paul
Andreas Masur
November 30th, 2002, 02:10 PM
Originally posted by KevinHall
Sleep() works great if you are using Windows. If you are using an OS with POSIX support, then use nanosleep(). You can usually find an example in the man pages.
- Kevin
Originally posted by PaulWendt
These guys made only correct statements. The only thing that I
want to add is that you have to look in the OS documentation.
This sort of thing is almost always going to be platform-specific
because of the different hardware involved. If you're in neither
windows nor a unix based OS [like if this is an embedded systems
program], you'll have to turn to the OS manual then.
Chances are, though, that Sleep(), sleep(), or nanosleep will help
you just fine.
--Paul
Ooopppsss....I somehow missed that this was posted in the Non-Visual-C++-Forum...thank you both for paying attention...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.