C # - Windows Service only runs once

1

I'm developing a windows service to run every 10 minutes, it inserts some values into the database, but I noticed that the routine only runs once.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Teste.BLL.Servidor;
using Teste.DTO.Servidor;
using Teste.Referencia.Email;

namespace Teste.AtualizaArmazenamentoServidores
{
    public partial class Service1 : ServiceBase
    {
        Timer timer;
        EventLog log = new EventLog();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();  //adicionado para conseguir deggugar no Visual Studio
            timer = new Timer(new TimerCallback(timer_Tick), null, 15000, TimeSpan.FromSeconds(40).Milliseconds); //coloquei 40 segundos para testar
        }

        protected override void OnStop()
        {
        }

        public void timer_Tick(object sender)
        {
            ExecutarTarefa();
        }

        public void ExecutarTarefa()
        {
            try
            {
                new ServidorBLL().SalvaDadosServidoresBanco();
            }
            catch (Exception ex)
            {
                SendEmail("Atualiza Servico", ex);
            }

        }
    }
}

The Routine works, the data is updated in the right database, but in the debug, the method is not called again after the first execution.

Thanks to anyone who can help.

    
asked by anonymous 10.07.2017 / 22:00

2 answers

0

Following Renan's guidance, the following operating code follows:

using System.Windows.Threading;
using System.Threading;
using System.Timers;

public partial class Service1 : ServiceBase
{
    System.Timers.Timer timer = new System.Timers.Timer();
    EventLog log = new EventLog();
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        this.timer = new System.Timers.Timer(600000D);  
        this.timer.AutoReset = true;
        this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
        this.timer.Start();
    }

    private void timer_Elapsed(object sener, EventArgs e)
    {
        ExecutarTarefa();
    }

    public void ExecutarTarefa()
    {
        //tarefa
    }
}
    
11.07.2017 / 14:21
3

I suggest reading the official documentation . In addition there are some things here that need to be reviewed:

  • The timer should be a property of the Service1 class. Otherwise, it will be deleted from memory by the Garbage Collector after the OnStart method is executed. This may be the main reason for the behavior that the program has now.

  • Note that the Timer type inherits from Idisposable , so strongly recommend that you implement your disposition as well to avoid leaks in memory. >

  • Assign the AutoReset property of your timer instance to the true value. Otherwise, even holding the instance it will still run only once, as per the documentation on the link.

10.07.2017 / 22:15