Which is better of the two if I have to perform a cleanup task between 0800-1700hrs. The cleanup routine should be kicked in every 30 minutes.
I can put in a timer in a windows app which will fire every 30 minutes and check the time, if in the time frame, call cleanup rou开发者_运维百科tine or do the same thing as a windows service
or a windows scheduler which kicks in every 30 minutes and the app will check time and determine if its within the time frame, do cleanup and get off..
In the interests of keeping it simple, I would create a basic console application that performs the required cleanup. There would be no timer/scheduling code in this application.
I would then set up a windows schedule to run it at the required times/intervals. Also (as Fredrik says in the comments) you can easily run the console application manually if needed.
I wouldn't advise a creating a windows service unless you need the task to run when nobody is logged in, but then you also need to deal with running under different privileges from an interactive user. Also, a scheduled task can run when nobody is logged in anyway (thanks Rob).
You should take a look at the Quartz Enterprise Scheduler (open source) for .NET. You will find it on the Spring.NET web site.
Personally I think building a windows service that utilises the Quartz Enterprise Scheduler would be a tidy way of implementing what you have asked and would be independant of the windows scheduler. In my company windows scheduler is disabled on every machine including servers as some viruses exploit it to spread themselves.
Your trade offs are:
Windows App. Requires someone to be logged in and the app running.
Console App w/Windows Scheduler. Requires someone to be logged in.
Windows Service. If you do not need access to the logged in persons desktop and you need it to run whether someone is logged in or not, then this is the only option.
What everyone else said.
The only caveat is if you need to administer the task timing through through some type of interface (business operators want it to run starting at 0700, for example, or they want the ability to disable the job). In this case, you can manipulate the task scheduler programatically through Power Shell, or have it check a flag to see if it is suppose to run.
精彩评论