This isn't meant as a scheduler for just any job. The job that I need to schedule here is very specific for an application I'm writing for one of my clients.
The scheduler will run as a console application because it's comparatively lightweight. The application will be started with Windows (startup items in the Registry) and should always be running - much like a Windows service but not (mostly because I don't have a project template for a Windows service in VS 2013 Community).
The task this scheduler runs should be done at 4am every day.
Does it look like the code below matches this description of the requirements?
class MyScheduledTask
{
private static Timer _timer;
static void Main(string[] args)
{
SetTime();
}
private static void SetTime()
{
DateTime currentTime = DateTime.Now;
int interval = 0;
DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 12, 45, 0);
if (currentTime <= scheduleTime)
{
interval = (int)scheduleTime.Subtract(currentTime).TotalSeconds;
}
else
{
interval = (int)scheduleTime.AddDays(1).Subtract(currentTime).TotalSeconds;
}
_timer = new Timer(interval);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
StartTaskWork();
_timer.Interval = (24 * 60 * 60 * 1000);
}
}