Hi manish_rcc
please have a look at the modified Code and the comments:
public partial class Service1 : ServiceBase
{
Timer timer;
TimeSpan startingTime;
TimeSpan endingTime;
public Service1()
{
InitializeComponent();
}
private static void start_timer()
{
timer.Start();
}
protected override void OnStart(string[] args)
{
timer = new Timer();
timer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
startingTime = new TimeSpan(
Int32.Parse(ConfigurationManager.AppSettings["StartingTimeHour"]),
Int32.Parse(ConfigurationManager.AppSettings["StartingTimeMinute"]), 0);
endingTime = new TimeSpan(
Int32.Parse(ConfigurationManager.AppSettings["EndingTimeHour"]),
Int32.Parse(ConfigurationManager.AppSettings["EndingTimeMinute"]), 0);
}
public static bool ShouldExecute()
{
TimeSpan CurrentTime = DateTime.Now.TimeOfDay;
return ((CurrentTime > startingTime) && (CurrentTime < endingTime));
}
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
DateTime TodayDate = DateTime.Now;
if (TodayDate.DayOfWeek == DayOfWeek.Saturday || TodayDate.DayOfWeek == DayOfWeek.Sunday)
{
return;
}
else
{
if(ShouldExecute())
{
BasicBO.ComputeData();
}
}
}
catch (Exception ex)
{
string msg = ex.Message + " at " + DateTime.Now + " From timer_Elapsed method";
BasicBO.writeLog(msg, "UpdateVMErrorLog.txt");
}
}
protected override void OnStop()
{
string msg = "Service Stoped at : " + DateTime.Now;
BasicBO.writeLog(msg, "UpdateVMStopLog.txt");
}
}
You see - the part you good confused about is, that a timer already represents a Kind of "Loop" to check...
Kind regards
Johannes