0

I am trying to set a function to run at a specific time in a day in c#. This code seems to work but I am not so confident about it. Is there any better way around this?

this is my code

String thetimeis = DateTime.Now.ToString("HH:mm:ss");
DateTime alarmtimeStart = Convert.ToDateTime("12:00:00");
DateTime alarmtimeStop = Convert.ToDateTime("12:02:00");

if (Convert.ToDateTime(thetimeis) > alarmtimeStart && Convert.ToDateTime(thetimeis) < alarmtimeStop)
{
  MessageBox.Show(thetimeis);
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
themhz
  • 8,335
  • 21
  • 84
  • 109
  • 1
    Possible duplicate of [Alarm clock application in .Net](http://stackoverflow.com/questions/1493203/alarm-clock-application-in-net). – Omar May 05 '12 at 18:32
  • the problem is not the alarm application here. but the management of string and datetime –  May 05 '12 at 18:44

4 Answers4

4

If you're looking for a more robust solution to scheduling jobs I'd recommend using Quartz. For trivial jobs, it's probably overkill, but I've found it easy to use and much easier than rolling my own solution.

Dan Busha
  • 3,723
  • 28
  • 36
2

Yes, there's a better way - just compare hours, minutes and seconds individually.

DateTime now = DateTime.Now;
DateTime alarmtimeStart = Convert.ToDateTime("12:00:00");
DateTime alarmtimeStop = Convert.ToDateTime("12:02:00");

if (now.Hour >= alarmtimeStart.Hour && now.Minute >= alarmtimeStart.Minute && now.Second >= alarmtimeStart.Second && now.Hour <= alarmtimeStop.Hour && now.Minute <= alarmtimeStop.Minute && now.Second <= alarmtimeStop.Second)
{
    MessageBox.Show(thetimeis);
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • lol I wonder if we also put milliseconds, will the execution time of the "if checks" overtake the current time, will it ever enter the if statement :P. Thanx – themhz May 05 '12 at 18:36
  • 2
    @themhz: Unless you're running on an 8080, the `if` should only take a matter of nanoseconds :) – Ry- May 05 '12 at 18:39
  • DateTimes support the `<` [operator](http://msdn.microsoft.com/en-us/library/system.datetime.op_lessthan.aspx), so there is no need to compare them component-by-component. – Douglas May 05 '12 at 18:40
  • @Douglas: That will compare the date as well. The point is that we're only looking to compare the time. But see BlueMonkMN's answer for the right way. – Ry- May 05 '12 at 18:41
  • Yes, then `TimeOfDay`is better. If the date is never important, then [TimeSpan.Parse](http://msdn.microsoft.com/en-us/library/se73z7b9.aspx) could be used instead of `Convert.ToDateTime`. – Douglas May 05 '12 at 18:48
2

Can't you directly compare the TimeOfDay properties of the dates?

Edit:

TimeSpan thetimeis = DateTime.Now.TimeOfDay;
TimeSpan alarmtimeStart = new TimeSpan(12, 0, 0);
TimeSpan alarmtimeStop = new TimeSpan(12, 2, 0);

if (thetimeis >= alarmtimeStart && thetimeis < alarmtimeStop)
{
   MessageBox.Show(thetimeis);
}
BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
  • can you demonstrate what your saying? I need to set manually the time of the alarm each time. And in some days it could change – themhz May 05 '12 at 18:46
1

I am bit confused why you need so much conversion, is it not better the following?

if (DateTime.Now > alarmtimeStart && DateTime.Now < alarmtimeStop) 
{ 
  MessageBox.Show(thetimeis); 
}