2

I am working on a project that allows me to create a program and give it tasks. Right now I am working on where I can tell it to set an alarm. The alarm feature works well, but I have a problem. When I set the alarm, I can say "goodnight" and then the program turns my monitor off into sleep mode. When the alarm goes off I want it to turn the monitor back on 10 or 15 seconds before it executes the code that happens when the alarm goes off.

Here is a bit of my code:

DateTime now = DateTime.Now;
string time = now.GetDateTimeFormats('t')[0];

if (time == Settings.Default.Alarm && Settings.Default.AClockEnbl == true)
{ 
    SetMonitorInState(MonitorState.MonitorStateOn); 
}
if (time == Settings.Default.Alarm && Settings.Default.AClockEnbl == true)
{ 
    //All other alarm code 
}

I want the first if statement to tell the command to happen 10 seconds before Settings.Default.Alarm

Can anybody help?

Chris Leyva
  • 3,478
  • 1
  • 26
  • 47
Cody_T
  • 81
  • 12

2 Answers2

0

If you add 15 seconds to the DateTime now and compare that to the Alarm time, you can use that to turn on the monitor.

Then you can execute the rest of the code when now (without the extra 15 seconds) equals the alarm time.

As mentioned in comments you'll want to compare with DateTime's instead of strings and use TimeSpan to increment 15 seconds.

Josh
  • 730
  • 7
  • 14
  • 1
    True, but he'll need a bit more help to do that. He's comparing time as a string(!). – Rup Dec 19 '13 at 19:23
  • I used your advice and it works good. before, when it was being turned on at the same time of the other code happening, the other code was not happening in time because the monitor needed time to load. This helps a lot. Thanks. – Cody_T Dec 19 '13 at 19:48
0

You can use a TimeSpan to offset your date:

beforeTime = now + TimeSpan.FromSeconds(15);

then compare Settings.Default.Alarm with beforeTime.GetDateTimeFormats('t')[0] in order to turn on the monitor.

This said, try to refactor your code and get rid of those strings. Use DateTime and TimeSpan or ms since Epoch in an int if you really want, but not strings.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133