Windows Service or Task Scheduler for maintenance tasks?

I have a C # application that performs some maintenance tasks. It should work approximately every hour, although it is not too important if it is turned off a little. And it should run on the Win2003 server without registering.

Basically, I wonder if I should write a Windows service, and if so, if Thread.Sleep () is the right way to pause Thread for an hour or if there are better ways to make sure Thread remains inactive and does not load the server? (aka what is the most opposite of spinlock)

An alternative is the Windows task scheduler, but I'm not sure if this is good for use on the server, because: a) I will need to save the schedule in it, and not in my app.config, b) I cannot easily control the service through start / stop / restart and c) I do not know if the Windows user credentials are as secure as when entering them into the Snapin MMC services.

What are your opinions? Is it possible and useful to have an unoccupied service, or would you recommend a task scheduler?

+6
windows-server-2003 windows-services
source share
6 answers

You may find this video interesting. A good interview with the engineer in charge of Windows services, and also comes when you need to choose a service or task. In a nutshell, if your function falls into the periodic maintenance category, complete the task.

+7
source share

I prefer the task scheduler itself, as it is easier to maintain and make changes to the schedule if you need to.

In addition, utilities that run continuously and “sleep” run the risk of causing problems if the developer “forgets” to do things like tight connections, files, etc. that can accumulate over time. The presence of the program "start and exit" is an additional protective blanket. :-)

+5
source share

If you go to the service route, I would recommend using System.Threading.Timer. With this, you can configure it to trigger an event once per hour. You can put the interval in app.config if you think you will ever need to change it.

Services also run under the local system account (by default), while you must specify a username / password when using scheduled tasks. If you use your username, this is a maintenance problem if you ever change your password and forget to update the task.

I have a situation where I use a combination of both. The service runs continuously throughout the day, but each week they maintain a network and database. So, I use two scheduled tasks. One to close the service at midnight and one to turn it on at 4 a.m. This is done by calling .BAT files with NET STOP / NET START commands.

+1
source share

I think the Windows service is probably more reliable and easy to monitor, but it will not offer the same planning flexibility as a scheduled task out of the box. In any case, I would go to the service, since most enterprise applications tend to work as services, not scheduled tasks.

For recent projects, I used Quartz.NET to configure scheduled processing in Windows Services. It's actually not that hard to set up (a good tutorial), and CronTrigger will give you a ton of flexibility with a tiny configuration. I like it better than the raw .NET timer.

+1
source share

I believe it’s worth spending 5 cents using the task scheduler to save some memory when your task is not running.

+1
source share

The Windows service is more secure: no one can lose it and ever work. I found a lot of problems with windows tasks (not running, ...).

The Windows Task Scheduler is for end-user tasks, not application tasks. Anyway, Windows Backup uses it ,-)

0
source share

All Articles