Introduction
Crate a a Simple window service and scheduling the windows service with
some time interval is very easy.but if you get requirement to schedule a
windows service daily at 10Am or some specific hours Or every weekly
once .Then you will search code in Google but you may not get exact code
in Google.Below is article which may help you while scheduling windows
service Daily,Weekly or monthly.
Background
As you know to schedule a windows service , we must require timer and
Timer has timer interval Property . we have to trigger an event when
timer interval Elapsed and timer has ElapsedEventHandler where we can
call method to work on.
Using the code
Explain the topic of the article with code and description
Block of code should be set style as "Code" like below.
using SunilWindowsServiceScheduler;
protected override void OnStart(string[] args)
{
WindowsServiceSchedler(_timer);
}
protected override void OnStop()
{
_timer.Stop();
}
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
#region Process ProcessFeefoResults
_timer.Stop();
WindowsServiceSchedler(_timer);
#endregion
}
catch (Exception)
{
_timer.Start();
}
}
private static void WindowsServiceSchedler(System.Timers.Timer _timer)
{
string _runweekly = Convert.ToString(ConfigurationSettings.AppSettings["Weekly"]);
string _weeklyeventTriggerTime = Convert.ToString(ConfigurationSettings.AppSettings["WeeklyeventTriggerTime"]);
string _dayOfWeek = Convert.ToString(ConfigurationSettings.AppSettings["DayOfWeek"]);
DayOfWeek MyDays = (DayOfWeek)DayOfWeek.Parse(typeof(DayOfWeek), _dayOfWeek);
string _DailyEventTriggerTime = Convert.ToString(ConfigurationSettings.AppSettings["DailyEventTriggerTime"]);
Scheduler sch = new Scheduler("SunilWindowsService");
if (_runweekly == "true")
{
sch.ScheduleWeekly(MyDays, _weeklyeventTriggerTime, _timer);
}
else
{
sch.ScheduleDaily(_DailyEventTriggerTime, _timer);
}
}
<!-- Set Event Trigger Timer in 24 hours format -->
<!--Configure Window Service Daily -->
<!-- Example : for 10:00 PM value="22" -->
<add key="DailyEventTriggerTime" value="10" />
<!--Configure Window Service Weekly -->
<!-- Example : for 10:00 PM value="22" -->
<add key="Weekly" value="false" />
<add key="DayOfWeek" value="Tuesday" />
<add key="WeeklyeventTriggerTime" value="10" />
Certified Scrum Master and Certified Scrum Product Owner with broad knowledge and experience in Agile and Scrum methodologies.Expertise in DEVOPS to increases an organization's ability to deliver applications and services at high velocity: evolving and improving products at a faster using Continuous Integration, Continuous Delivery, Continuous Testing, and Continuous Monitoring.