C # - Windows Service only runs once

I am 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 debug, the method is not called again after the first run.

Thank anyone who can help.

Author: Guilherme Golfetto, 2017-07-10

2 answers

I suggest reading the official documentation . Also there are a few things here that need to be reviewed:

  • The timer must be a property of the Class Service1. 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 type Timer inherits from Idisposable , so I strongly recommend that you implement your layout also to avoid leakless in memory.

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

 3
Author: Garoto de Programa, 2017-07-10 20:15:52

Following the guidance of Renan, follows operational code:

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
    }
}
 0
Author: Guilherme Golfetto, 2017-07-11 12:21:47