I have created an application that is used to read a mail box at certain intervals. If there is a new mail it downloads the attachment creates pdf files say 100 + combines it and mail it back to a particular list. Due to some server policies am in a position to convert it to a window service. I have used a timer my code given below
private System.Threading.Timer timer;
timer = new System.Threading.Timer(TimerTick, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
void TimerTick(object state)
{
var minute = DateTime.Now.Minute;
if (minute != lastMinute && minute % 5 == 0)
{
//check mail here
}
}
Is implementing a timer like this an efficient way of doing this? Is there any better way to handle this? I am worried about the performance because the applications need to run 24 x7 and hence can end up in utilizing more cpu memory if inefficient.
is timer the only best available option in this scenario ?
You should monitor you service for performance. If you see there a performance problem:
System.Threading.Timeris a simple, lightweight timer that uses callback methods and is served by threadpool threads.System.Timers.Timerfor server-based timerfunctionality.
Maintainability and debug tips:
This will simply give you or administrators control about the service.
Use the code for easy debugging your service: