I created a windows service with a timer in it, where I need to set set the interval after each Elapsed timer event. For example, I’d like it to fire on the hour every hour.
In Program.cs:
namespace LabelLoaderService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
static void Main()
{
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new LabelLoader()
};
ServiceBase.Run(ServicesToRun);
#else
LabelLoader ll = new LabelLoader();
ll.Start();
#endif
}
}
}
In LabelLoader.cs:
namespace LabelLoaderService
{
public partial class LabelLoader : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
public LabelLoader()
{
InitializeComponent();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
protected override void OnStart(string[] args)
{
SetTimer();
}
public void Start()
{
// Debug Startup
SetTimer();
}
public void SetTimer()
{
DateTime nextRunTime = GetNextRunTime();
var ts = nextRunTime - DateTime.Now;
timer.Interval = ts.TotalMilliseconds;
timer.AutoReset = true; // tried both true & false
timer.Enabled = true;
GC.KeepAlive(timer); // test - no effect with/without this line
}
void timer_Elapsed(object source, ElapsedEventArgs e)
{
timer.Enabled = false;
// do some work here
SetTimer();
}
If I installutil this onto my local machine, it correctly determines the next run time and executes. But it doesnt run anytime after that. If I restart the service it runs the next scheduled time and then nothing again. Is there an issue calling SetTimer() at the end of my processing to reset the Interval and set timer.Start()?
use
System.Threading.Timerinstead – in my experience it is more suited for server-like use…EDIT – as per comment some code/hints:
the following is a very basic way to avoid reentry (should work ok in this specific case) – better would be some
lock/Mutexor similarmake
nextRunTimean instance fieldcreate/start your time with for example
create your timer handler similar to