A timer in a thread in a Windows service

I cannot figure out how to best solve this problem.

Now I have a Windows service whose task is to collect data from a database with a specific DSN, and then send email if the data is valid. The service contains a timer that goes off every 5 minutes and performs the above tasks.

Now I need to overwrite the Windows service in order to be able to run more than 1 DSN. I was thinking of making several threads inside a windows service, and then turning on the seperat timer in each thread again. This is a good idea and how can this be done? I want you to have no Windows service for every DSN.

Try to draw it if I don't feel the point

                             Windows Service

Thread1 (DSN1) ----------------------------- Thread2 (dsn2) ---------- --- --------- Thread3 (DSN3)
Timer (ticks every X minutes) ----------------- Timer (same) -------- -------------- --- Timer (same)
Logic () ------------------------ --------------------- logical unit --------------------------- ----- Logic ()

Hope my problem makes sense :)

+3
source share
4 answers

As far as I know, each timer is a thread in itself. Knowing this, I would try to dynamically create timer objects for each given dsn.

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    private List<GetDataFromDSN> list = null;
    protected override void OnStart(string[] args)
    {
        list = new List<GetDataFromDSN>();
        // assume args contains your given dsn values
        foreach (string dsn in args)
        {
            GetDataFromDSN newObj = new GetDataFromDSN();
            newObj.DSN = dsn;
            list.Add(newObj);
            newObj.Start();
        }
    }
}

public class GetDataFromDSN
{
    public string DSN { get; set; }
    private Timer timer = null;
    private double interval = 1000*60*5; // 5 minutes interval
    public GetDataFromDSN()
    {
        // init your object
        timer = new Timer(interval);
        timer.Elapsed +=new ElapsedEventHandler(timer_Elapsed);
    }
    private void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        // do what ever you want
    }
    public void Start() // or even make timer public
    {
        timer.Start();
    }
    public void Stop()
    {
        timer.Stop();
    }
}
+5
source

Does each DSN need to be in a separate thread?

- , Thread, , DSN, . , IEmailService :

public interface IEmailService
{
    void SendEmailsToValidAddresses();
}

:

public class MultipleSourcesEmailService : IEmailService
{
    private IEnumerable<IDatabaseSource> databases;
    public EmailService(params IDatabaseSource[] sources)
    {
        databases = new List<IDatabaseSource>(sources);        
    }

    public void SendEmailsToValidAddresses()
    {
        foreach(var database in databases)
        {
            var emailAddresses = database.SelectAllEmailAddresses();
            ValidateAndSendEmailsTo(emailAddresses);
        }
    }

    public void ValidateAndSendEmailsTo(IEnumerable<string> emailAddresses)
    {
        // Perform appropriate logic
        ...
    }

}        

, , IEmailService. , SingleSourceEmailService MultipleSourceEmailService , , .

, EmailService, , SendEmails - , , EmailService, DSN, , : MultiThreadedMultipleSourceEmailService, IEmailService .

+4

System.Threading.Timer

, ,

public void StartDSNTimers()
    {
        _tmr1 = new Timer(CheckMessages, dsn1, 0, 60000);
        _tmr2 = new Timer(CheckMessages, dsn2, 0, 60000);
        _tmr3 = new Timer(CheckMessages, dsn3, 0, 60000);
    }

    private void CheckMessages(object obj)
    {
        //Logic
    }
0

Source: https://habr.com/ru/post/1546526/


All Articles