I am using Timer in Windows Service on OnStart()
method handling timer_Elapsed()
event check after every 5 min.
But timer_Elapsed()
not firing/calling after 5 min, Please correct me If I am doing anything wrong in below code.
protected override void OnStart(string[] args)
{
try
{
genLogs.WriteErrorLog("Service Started OnStart.");
//int tmStart = Convert.ToInt32(ConfigurationManager.AppSettings["tim"]);
using (Timer timer = new Timer(30000)) // (1000 * 5 * 60) for 5 minutes
{
timer.Elapsed += timer_Elapsed;
timer.Start();
//Console.WriteLine("Timer is started");
genLogs.WriteErrorLog("Timer is started.");
//Console.ReadLine();
}
}
catch (Exception ex)
{
genLogs.WriteErrorLog("OnStart Service Error: " + ex.Message);
}
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
genLogs.WriteErrorLog("Service Started Checking Windows Services and Web AppPools.");
serviceLogic.InsertStatus();
genLogs.WriteErrorLog("Service Calls Send SendEmails Method.");
serviceLogic.SendInfo();
}
catch (Exception ex)
{
genLogs.WriteErrorLog("OnStart Service Error: " + ex.Message);
}
}