I've created a simple daily task scheduler but I'd like to change this to a general task scheduler accepting more advanced schedules.
I'd like to have some critiques from other sets of eyes on my current implementation before I continue.
public class ScheduleJob
{
#region PRIVATE VARIABLES
private readonly Timer _timer;
private Action _job;
private int _setHour;
private int _setMin;
#endregion
#region CONSTRUCTOR
public ScheduleJob()
{
_timer = new Timer();
_setHour = 0;
_setMin = 0;
}
#endregion
#region PUBLIC METHODS
public void SetTimeofDay(int in_hour, int in_min)
{
_setHour = in_hour;
_setMin = in_min;
}
public void SetJob(Action in_task)
{
_job = in_task;
}
public void Start()
{
try
{
int PeriodMin = GetHour() * 60 + GetMin(); //minute
_timer.Interval = PeriodMin * 60 * 1000; //millisecond
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
IsRunning = true;
}
catch (Exception)
{
IsRunning = false;
}
}
public void Stop()
{
if (_timer != null)
_timer.Stop();
IsRunning = false;
}
#endregion
#region PRIVATE METHODS
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_timer.Enabled = false;
_job(); //perform the job
int PeriodMin = 24 * 60;
_timer.Interval = PeriodMin * 60 * 1000;
_timer.Enabled = true;
}
private int GetHour()
{
int _nowHour = DateTime.Now.Hour;
if (_nowHour > _setHour)
{
return (24 - (_nowHour - _setHour));
}
else
{
return (_setHour - _nowHour);
}
}
private int GetMin()
{
int _nowMin = DateTime.Now.Minute;
if (GetHour() == 0)
{
if ((_setMin - _nowMin) <= 0)
{
return (24 * 60) + Math.Abs(_setMin - _nowMin);
}
}
return (_setMin - _nowMin);
}
#endregion
#region PROPERTIES
public bool IsRunning { get; set; }
#endregion
}