Need help writing a recurring task scheduler
I need to write a tool that will run a recurring task on a user configurable schedule. I'll write it in C# 3.5 and it will run on XP, Windows 7, or Windows Server 2008. The tasks take about 20 minutes to complete. The users will probably want to set up several configurations: e.g, daily, weekly, and monthly cycles. Using Task Scheduler is not an option.
The user will schedule recurrences th开发者_开发百科rough an interface similar to Outlook's recurring appointment dialog. Once they set up the schedule they will start it up and it should sit in the system tray and kick off its tasks at the appointed times, then send mail to indicate it has finished.
What is the best way to write this so that it doesn't eat up resources, lock up the host, or otherwise misbehave?
The best thing you can do is avoid reinventing the wheel by leveraging what the OS provides. See the article Calling the Task Scheduler in Windows Vista (and Windows Server 2008) from managed code from Bart De Smet for a very to-the-point usage of the windows task scheduler from C#.
If for some reason you must implement your own service (e.g. greater control over dependencies), I'd look at some of the open source Cron implementations in .NET compatible languages. There is an article with code on code project named Implementing a small Cron service in C# that seems to do this. Based on his article An Event Based Cron (Scheduled) Job In C#, it looks like Bob Cravens has done some more thorough work in this area or at least documented his work more thoroughly. I'm sure there are others if you look around, and something based on Cron is a good starting point.
I will suggest writing a Windows Service that uses a SQlite db to get the schedules and add a GUI to write whatever you need to the db but remember to use
System.Timers.Timer
and not the
System.Windows.Forms.Timer
In your Windows Service
精彩评论