I need to write some .NET code that runs on a regular interval to check for "unprocessed" items in a table in the database, processes them, and then writes back to the database that they are processed. We'll probably set it up to run every minute.
How can I set up in the component in a Windows Server environment so that the code executes on a periodic basis?
Use System.Timers.Timer in your windows service.
You can have such a code (from MSDN) in your windows service OnStart event :
// Hook up the Elapsed event for the timer.
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
timer.Interval = 2000;
timer.Enabled = true;
...........
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
......
}
If the interval is not frequent for example if it runs daily also consider using Windows Task Scheduler. It can run your application for the given interval (note if that interval is not so frequent).