![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
way to make a function execute at time 'x'
Is there a way to make a function's execution wait till a specific system time? example. I want the end user to choose from 3-4 times (time of day) to make a function run (shutdown or restart computer). The fourth choice maybe 1.5 hours from the time that the user would be prompted to make the choice. Is there a way to make the program check the current system time often enough to make the program execute properly, but not hog up a ton of CPU cycles?
The best solution I have come up with would be done by making the program sleep for approx. 30 seconds then run an IF statement to see if the user defined time = the current system time (+/- 30 sec) and loop until that value is reached. Would this work sufficiently in your opinion, or is there another way? To my knowledge, Sleep uses little to no CPU time. If this is the case, then in theory, my example will use very little CPU resources while running the time check loop. So I guess my last question is. Can you actually designate a time and have the program reference that to the current system time then execute a function if the two are equal (in this case equal +/- 30 sec.)? If I was not descriptive enough, please let me know and I will try and explain better. Thanks for the help, -BB98
__________________
Learning to use C++ and loving every minute of it. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2006
Posts: 60
Rep Power: 3
![]() |
I think using sleep the way you describe sounds like a good solution. So what are you going to do, delete a few random files now and then?
![]() |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
Quote:
Thanks, -BB98
__________________
Learning to use C++ and loving every minute of it. |
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Such things are OS dependent. So tell us your target OS.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
You can use the Win32 Sleep function to do this. Alternatively, you can set up a timer that'll execute your code after every few seconds.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Any reason you don't just use the OS's task scheduler to run your reminder programs at the appropriate times? The people who wrote it know all the tricks you'd have to learn to be as efficient.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
The Windows task scheduler is erratic at best, DaWei.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
I suppose I could make the program send a system command using "at" to make it setup a one-time-only task. to run at the desired time. I would just rather use it in my own program becuase using the "at" command to schedule a system shutdown or restart brings up the shutdown.exe window with a 30sec timer. I want it to be more like a 5-10sec timer, if any. Which I can control when using the InitiateSystemShutdown() command. Unless you guys know of a way to control the length of the timer from the command prompt.
__________________
Learning to use C++ and loving every minute of it. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
Guess this bring on another question... How do you compare times? any references, or examples?
Thanks, -BB98
__________________
Learning to use C++ and loving every minute of it. |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Mar 2006
Posts: 60
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|