Reputation: 152
In my Window Service application the timer function is never called. I deployed the service on my machine and then attached the service to Visual Studio with breakpoints on different function. After OnStart()
none of my functions are called.
Here's my OnStart()
function
protected override void OnStart(string[] args)
{
this.timer = new System.Timers.Timer(50000);
this.timer.AutoReset = true;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
this.timer.Start();
}
Updated 09/12/2017 : Added Event log in try and catch
This function then calls timer1_tick()
:
private void timer1_Tick(object sender, EventArgs e)
{
try
{
EventLog.WriteEntry("Running.."+ e.ToString());
}
catch (Exception ex)
{
EventLog.WriteEntry("Caught Exception "+ex.ToString(), EventLogEntryType.Error);
}
}
The timer1_tick
is never called. Any suggestions?
Update 12/09/2017 : I checked the Event log . Under Windows Logs > Application, I can see the message , service started successfully . However , the event logs added in the try-catch block aren't get displayed. Not sure if I am missing something.
I attached the service to Visual Studio for debugging. In the below image, the OnStart()
method is called. I just added the Thread.Sleep(30000)
so that I get some buffer time to attach the process to the debugger to avoid skipping the OnStart()
function
After the Timer starts, I have a breakpoint on the TimerTick() function expecting it to be hit, but it never does
Upvotes: 1
Views: 994
Reputation: 4950
timer1_Tick
is likely being called as everything in your OnStart
seems appropriate. Perhaps the reason you aren't receiving e-mails is because _SendEmail
is throwing the exception.
Writing to the EventLog
might provide more information?
private void timer1_Tick(object sender, EventArgs e)
{
try
{
SendMail._SendMail("Processing A started at" + DateTime.Now.ToString());
Logging.log.LogInput("start refund process");
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
}
}
As a best practice I would also write to the EventLog
in OnStart
and OnStop
.
protected override void OnStart(string[] args)
{
// Log a service start message to the Application log.
EventLog.WriteEntry("My Service in OnStart.");
timer = new System.Timers.Timer(50000);
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer.Start();
}
protected override void OnStop()
{
// Log a service stop message to the Application log.
EventLog.WriteEntry("My Service in OnStop.");
}
Upvotes: 2