C# - Windows Service only runs once

Asked

Viewed 681 times

1

I am developing a windows service to run every 10 minutes, it inserts some values in 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);
            }

        }
    }
}

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

I thank anyone who can help.

2 answers

3

I suggest reading the official documentation. Besides there are some 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 execution of the method Onstart. This may be the main reason for the behavior the program now has.

  • Note that the type Timer inherits from Idisposable, then strongly recommend that you implement your provision also to avoid leaks in memory.

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

0


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
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.