Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2006, 11:29 AM   #1
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
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.
badbasser98 is offline   Reply With Quote
Old Apr 4th, 2006, 11:43 AM   #2
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
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?
Xyhm is offline   Reply With Quote
Old Apr 4th, 2006, 11:46 AM   #3
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Such things are OS dependent. So tell us your target OS.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 4th, 2006, 12:14 PM   #4
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Quote:
Originally Posted by Xyhm
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?
No, this is a program that I will be using for my company that is scheduled to run 15 minutes before the store closes to remind the user to shutdown their PC. Since we tell them all the time and they never remember. I have created a GUI interface that runs with a few buttons and menu options. I want one option to be "Shutdown at". When the user selects this option they are given four choices of when they want the computer to shut down (differences between summer and winter hours). Thus why I wanted to reference the system time. This will be running on all Windows XP Pro systems.

Thanks,
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Apr 4th, 2006, 12:25 PM   #5
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Apr 4th, 2006, 12:33 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 4th, 2006, 12:37 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The Windows task scheduler is erratic at best, DaWei.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 4th, 2006, 1:00 PM   #8
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
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.
badbasser98 is offline   Reply With Quote
Old Apr 4th, 2006, 1:49 PM   #9
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
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.
badbasser98 is offline   Reply With Quote
Old Apr 4th, 2006, 2:03 PM   #10
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
Quote:
Originally Posted by badbasser98
Guess this bring on another question... How do you compare times? any references, or examples?
If you're using the functions from time.h, you can get some info here (including examples from the function difftime, which compares times). But that's a C library so it might not be what you use, but perhaps you can still get some idea from there.
Xyhm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC