Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

Timer on windows service doesn't work on the server

I have a windows service that uses a timer. The timer works every some seconds and check for some changes in the database and do then if any changes, the windows service does do actions.

I did everything, with a heavy testing on my machine, on my machine, the windows service is working perfectly.

When I deployed on the server, the windows service checks for database changes just when i start the windows service, but it doesn't keep itself checking for any changes in the database. I don't know the problem and I have been stick here for three days in a row, I checked everything.

On my machine I deploy using framework 32 and on the server I also deploy using framework 32.

I also tried to deploy on the server using framework 64, but still the same problem.

I did a log for any failure and there is no error logs at all.

Could you give me any hint about where the problem could be raising?

I run the windows service with the timer like this:

private System.Timers.Timer timer;

public ServiceStatus()
{
    InitializeComponent();

}

protected override void OnStart(string[] args)
{
    try
    {
        this.timer = new System.Timers.Timer(30000);  // 30000 milliseconds = 30 seconds
        this.timer.AutoReset = true;
        this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
        this.timer.Start();
    }
    catch (Exception ee)
    {
        this.EventLog.WriteEntry("ERROR ERROR ERROR="+ee.Message);
    }
}

protected override void OnStop()
{
    this.timer.Stop();
    this.timer = null;
}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    new StatusHandler().check();
}

It has been three days and I am not able to solve, please any hint is appreciated.

Upvotes: 0

Views: 625

Answers (1)

Belogix
Belogix

Reputation: 8147

The issue is because the try / catch block in OnStart will only handle exceptions thrown in the code setting up the event. It will not catch any exceptions when the event itself runs.

If an exception is thrown in your StatusHandler().check() method the service will exit and nothing will be logged.

Therefore you need to add try / catch to your StatusHandler().check() method so that you can trap any exceptions, log them and handle them appropriately.

Upvotes: 1

Related Questions