Tasks or topics for continued work?

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);
    //EmailAnalyzer is a custom class I use to manage the accounts
    ea1 = new EmailAnalyzer(Config1); 
    ea2 = new EmailAnalyzer(Config2); 
    ea3 = new EmailAnalyzer(Config3); 
}

protected void EmailParserWorker(object state)
{
    try
    {
        // Pause timer during processing so it won't be run twice if the processing takes longer than the interval
        mailParserTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);

        // MANAGE THE THREE EMAIL ACCOUNTS
    }
    catch (Exception ex)
    {
        log.Error("[EmailParserWorker] Exception caught:" + ex.Message, ex.InnerException);
    }

    // Launch again in the specified time
    mailParserTimer.Change(SLEEP_TIME_MAIL_PARSER, Timeout.InfiniteTimeSpan);
}

, . , , , Threads Tasks, , . , ThreadPooling , - , , (, , ); Task ? , LongRunning, , . Threads, , , .

, , . Tasks, , LongRunning. , , , , - ?

+4
1

LongRunning " ". , .

Task LongRunning. Thread. .

- . 1 . 3 .

, , - ?

. , . . , .

+7

All Articles