Earlier I wrote about how you can debug Windows Services without leaving Visual Studio. Yay!

Another common issue that comes up when building Windows Services is the lack of common wrappers around the service container. One such scenario is a service that runs indefinitely and performs an action at fixed time intervals.

Below is some code I wrote to create a generic container to manage your Windows Service when it needs to run at a predetermined interval (eg. every 5 seconds). All you need to do is add and extend the TimedIntervalServiceBase.cs file and override IntervalInSeconds to the desired time interval you would like and you’re done.

The cool thing about TimedIntervalServiceBase is it guarantees the event firings are atomic so there is no overlap between invocations.

Plus it maps all of the standard operations you can perform on a Windows Service to the event firings. So if you stop or pause the service, the events will stop being fired at the time intervals.

2 Comments A Generic Time Interval Container for Windows Services

  1. Nick

    Have you looked at using the Quartz Scheduler for .NET? It a lot of functionality for controlling when processes in your windows services should run.

    1. Adam

      I came across Quartz in the past but haven’t looked into it recently. That would certainly make for a more elegant internal scheduling layer if the need arises. Thanks for reminding me of it.

Comments are closed.