I created a Windows service that runs some tasks from time to time so I used System.Threading.Timer. But on some occasions, I noticed that the function stops being called.
I checked by writing to a log file that you can check where the program is possibly stopping. All executions of the called function are finished, but at some point it stops being called
Here's my code:
namespace Service
{
public partial class Service: ServiceBase
{
protected override void OnStart(string[] args)
{
// do some stuff
log("Service Started"); // function that write in .txt file
// define delay and execution interval
Timer timer = new Timer(this.OnTimer, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(60));
}
public void OnTimer(object state)
{
log("Function fired");
// do something
log("Function closed");
}
protected override void OnStop()
{
log("Service stopped");
}
}
}
What do I need to investigate or do to ensure the function is always called? Thanks a lot!