1

I'm using a C# Windows Service application where I'm having a object containing a stored procedure.

public class Sp
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int IntervalMin { get; set; }
    public DateTime LastRun { get; set; }
}

Right now I'm using this method to check if it's time to run:

public bool TimeToRun()
{
    return ((IntervalMin * 60) <= (DateTime.Now - LastRun).TotalSeconds);
}

The problem I have is that when the service is down during weekends and wakes up on monday TimeToRun will be true for all (considering IntervalMin isn't really high).

For example, there is one Sp with IntervalMin = 1440 (should be runned once a day), and lets says LastRun is Friday 14:00, on monday when service wakes up at 03:00 it will run, but I want it to be as close as possible to 14:00.

You have any tips of how I can build this kind of schedule? Do I need to use more properties that I set? It's no problem if it's like +/- 5 minutes or even more. TimeToRun() is checked about every each second.

LastRun is currently set to Datetime.Now, but I would like to use AddMinutes to cause no extra delays.

LastRun = LastRun.AddMinutes(IntervalMin)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MrProgram
  • 5,044
  • 13
  • 58
  • 98
  • shouldn't you be using Task Scheduler- http://windows.microsoft.com/en-in/windows/schedule-task#1TC=windows-7 ? – Sangram Nandkhile Jun 24 '15 at 10:47
  • 2
    Don't reinvent the wheel. Scheduling is _hard_. Use existing libraries or simply use the Windows Task Scheduler. What exactly do you mean by _"service is down"_ and _"service wakes up"_, by the way? – CodeCaster Jun 24 '15 at 10:49
  • There are lots of duplicate questions. Have a look at the list of related questions at the right hand side. – M4N Jun 24 '15 at 11:05
  • @CodeCaster I run the application as a Windows Sevice, not as a Schedule Task. The reason is because some Sp:s need to have an interval of 1min, and the fastest occurrence you can have on a schedule task is every 5min. – MrProgram Jun 24 '15 at 11:15
  • @M4N Because of the service being up only Mon-Fri, and the Sp in my example is suppose to run every weekday at 14:00 I'm not sure that I have a duplicate...Can't find a solution for that, or am I missing something? – MrProgram Jun 24 '15 at 11:33

0 Answers0