views:

1022

answers:

6

I have a C# Application that does some maintenance tasks. It needs to run roughly every hour, although it's not too important if it's a bit off. And it has to run on a Win2003 Server with no one logged in.

Basically I wonder if I should write a Windows Service, and if yes, if Thread.Sleep() is a proper way to pause the Thread for an hour, or if there are better ways to make sure the Thread stays idle and does not put any server load? (aka. what is the most opposite of a Spinlock)

The alternative is the Windows Task Scheduler, but I am not sure if that is good for server use since a) i'd have to store the schedule in it rather than in my app.config, b) I cannot easily control the service through start/stop/restart and c) I do not know if the Credentials of the Windows User are as secure as when I enter it in the Service MMC Snapin.

What are your opinions? Is it possible and good to have an idle Service or would you recommend the task scheduler instead?

A: 

Windows service is more secure: no one can drop it and ever works. I've found a lot of troubles with Windows Tasks (not executing, ...).

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

FerranB
+2  A: 

I prefer the Task Scheduler myself, as it is easier to maintain and make changes to the schedule if you need to.

Also, utilities that run continuously and "sleep" run the risk of causing problems if the developer "forgets" to do things like close connections, files etc. which can build up over time. Having the program "run and exit" is an extra safety blanket. :-)

Ron

Ron Savage
+1  A: 

If you go the services route then I would recommend using System.Threading.Timer. With that you can set it to fire an event once an hour. You can put the interval in the app.config if you think you will ever need to change it.

Services also run under the local system account (by default) whereas you must supply a username/password when using scheduled tasks. If you use your username it becomes a maintenance issue if you ever change your password and forget to update the task.

I have a situation where I use a combination of both actually. The service runs constantly during the day but they do network and database maintenance every night. So, I use two scheduled tasks. One to shut the service down at midnight and one to turn it back on at 4am. This is done by calling .BAT files with NET STOP/ NET START commands.

Loki Stormbringer
+3  A: 

You may find this video interesting . Good interview with the engineer responsible for windows services and also goes into when to pick service or task. In a nutshell if your feature falls into the periodic maintenance category go with a task.

Matt Davison
+1  A: 

I think a windows service is probably more reliable and easier to monitor, but it won't offer the same scheduling flexibility as a scheduled task out-of-the-box. I'd probably go with the service anyway, since most enterprisey apps tend to run as services, not scheduled tasks.

For recent projects, I've used Quartz.NET to setup scheduled processing in windows services. It's actually not too hard to setup (good tutorial), and the CronTrigger will give you a ton of flexibility with only a tiny bit of configuration. I like it better than a raw .NET Timer.

Andy White
+1  A: 

I suppose to throw my 5 cents worth in, using task scheduler will save some memory when your task is not running.

Mark Schultheiss

related questions