I have a windows service that runs two methods; one uses the library Ae.Net.Mailto read unread emails from three email accounts every 5 minutes (call EmailParserWorker), and the other method does another unspecified job every 30 minutes. Since these methods need to be run every X times, I use Timerto manage them, and they work fine.
private Timer mailParserTimer;
private readonly TimeSpan SLEEP_TIME_MAIL_PARSER = TimeSpan.FromSeconds(300d);
public MyService()
{
ServiceName = _serviceName;
mailParserTimer = new Timer(new TimerCallback(EmailParserWorker), null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
}
public void StartService()
{
mailParserTimer.Change(SLEEP_TIME_MAIL_PARSER, Timeout.InfiniteTimeSpan);
ea1 = new EmailAnalyzer(Config1);
ea2 = new EmailAnalyzer(Config2);
ea3 = new EmailAnalyzer(Config3);
}
protected void EmailParserWorker(object state)
{
try
{
mailParserTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
}
catch (Exception ex)
{
log.Error("[EmailParserWorker] Exception caught:" + ex.Message, ex.InnerException);
}
mailParserTimer.Change(SLEEP_TIME_MAIL_PARSER, Timeout.InfiniteTimeSpan);
}
, . , , , Threads Tasks, , . , ThreadPooling , - , , (, , ); Task ? , LongRunning, , . Threads, , , .
, , . Tasks, , LongRunning. , , , , - ?