Thanks for reading by the way
if time = x
Page 1 of 15 Replies - 253 Views - Last Post: 08 July 2011 - 08:18 AM
#1
if time = x
Posted 07 July 2011 - 10:54 PM
Recently In my chronicles of coding, I have began developing things that are actually useful. This app does multiple things, but the shutdown at X time isn't going so well. I want to specify the time for shutdown and when the time has been reached it will shutdown. I have figured up a way to do it, I will have a timer that will tick every 10 seconds, and run the code to check if the time is X and if it is, it will attempt to shut down the computer. However I have been having trouble figuring out what if statement to use, any suggestions?
Thanks for reading by the way
Thanks for reading by the way
Replies To: if time = x
#2
Re: if time = x
Posted 07 July 2011 - 11:12 PM
I do something along those lines in most of my programs.
In the Program.cs file I rase an event every 1 second. The event is 'Heartbeat'. All other classes subscribe to that event and use it for sync'ing.
This keeps me from having a bunch of separate timers that are all slightly (5-95 milliseconds) out of sync with each other.
Anyway, something like this should work - though you need to adjust for your code that you didn't supply. This shows based on a HeartBeat event, but could just as easily be your Timer_Tick event.
In the Program.cs file I rase an event every 1 second. The event is 'Heartbeat'. All other classes subscribe to that event and use it for sync'ing.
This keeps me from having a bunch of separate timers that are all slightly (5-95 milliseconds) out of sync with each other.
Anyway, something like this should work - though you need to adjust for your code that you didn't supply. This shows based on a HeartBeat event, but could just as easily be your Timer_Tick event.
void ProgramHeartbeat(object sender, eventargs e)
{
if (DateTime.Now > ShutDownDateTime)
{
DoShutDownMethod();
}
}
#3
Re: if time = x
Posted 07 July 2011 - 11:33 PM
that is exactly what im looking for, but im slightly confused about my if,
if (DateTime.Now == im uncertain about the format I should put it in, say I want 6 pm
if (DateTime.Now == im uncertain about the format I should put it in, say I want 6 pm
#4
Re: if time = x
Posted 08 July 2011 - 02:07 AM
You're going to hard-code the shut down time into the code?
Don't you have a DateTime variable the user is setting, that you are saving at shutdown and restoring at Load?
Anyway...
You have to compare Apples with Apples and Oranges with Oranges.
So if you are going to use DateTime.Now you need to compare with another DateTime object.
Of course you could just use part of that.
But there are possible complications with that.
If you set your showdown time for Midnight exactly it won't work because hour 00 is not greater than hour 23
So yeah, you have to do a little work. Such as check the DateTime at launch, compare to the dateTime now and see if the hour is great than the desired hour - OR - if the Date is because that would mean you rolled over midnight.
You might actually have to get creative and not expect a simple 1 line 'if' statement.
Read up on MSDN about DatTime so you can see just how much is available to you with it. There is a lot.
Don't you have a DateTime variable the user is setting, that you are saving at shutdown and restoring at Load?
Anyway...
You have to compare Apples with Apples and Oranges with Oranges.
So if you are going to use DateTime.Now you need to compare with another DateTime object.
Of course you could just use part of that.
If (DateTime.Now.Hour > 17)
{
// When the hour hits 1800 then it
// will be greater than 17
// Shutdown
}
But there are possible complications with that.
If you set your showdown time for Midnight exactly it won't work because hour 00 is not greater than hour 23
So yeah, you have to do a little work. Such as check the DateTime at launch, compare to the dateTime now and see if the hour is great than the desired hour - OR - if the Date is because that would mean you rolled over midnight.
You might actually have to get creative and not expect a simple 1 line 'if' statement.
Read up on MSDN about DatTime so you can see just how much is available to you with it. There is a lot.
#5
Re: if time = x
Posted 08 July 2011 - 04:36 AM
Agreed, I worte a similar program a year or so ago just for personal use that took a variety of "shutdown criteria" - time, timespan, process ended, cpu usage, ram usage, file modified, etc and allowed the user to combine them into queuires to say "If in 10 minutes from now the cpu usage is less than 2% or some specified process has finished then shutdown/restart/log out the computer", and these criteria were check by a timer that ticked at a specified rate (1 second, 10 second, 30 second, 60 second etc). I never actually used the program for much, but I woulde defiently use a timer, and I particulary like tlhIn`toq's program heartbeat method.
#6
Re: if time = x
Posted 08 July 2011 - 08:18 AM
Ticon, on 07 July 2011 - 11:33 PM, said:
if (DateTime.Now == im uncertain about the format I should put it in, say I want 6 pm
Just wanted to add that rarely, if ever, will you want to use == for checking the time. With system delays and such, it's very easy to miss the millisecond that you'd need to hit. Use > as tlhIn`toq showed.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|