views:

4363

answers:

7

What are the cons and pros of windows services vs scheduled tasks for running a program repeatedly (e.g. every two minutes)?

A: 

A Windows service doesn't need to have anyone logged in, and Windows has facilities for stopping, starting, and logging the service results.

A scheduled task doesn't require you to learn how to write a Windows service.

Mark Ransom
Scheduled tasks can also run without the user being logged in, but the user's password must be provided to the scheduling agent.
moodforaday
+5  A: 

What's the overhead of starting and quitting the app? Every two minutes is pretty often. A service would probably let the system run more smoothly than executing your application so frequently.

Both solutions can run the program when user isn't logged in, so no difference there. Writing a service is somewhat more involved than a regular desktop app, though - you may need a separate GUI client that will communicate with the service app via TCP/IP, named pipes, etc.

From a user's POV, I wonder which is easier to control. Both services and scheduled tasks are pretty much out of reach for most non-technical users, i.e. they won't even realize they exist and can be configured / stopped / rescheduled and so on.

moodforaday
+1  A: 
  1. It's easier to set up and lock down windows services with the correct permissions.
  2. Services are more "visible" meaning that everyone (ie: techs) knows where to look.
Chris Lively
Your first point also applies to scheduled tasks. I'm not sure what "more visible" means. Scheduled tasks can be viewed just as easy as services.
w4g3n3r
@w4g3n3r: Most tech people know how to look at windows services to see what is running. Further, if it's a service (and running) it will show up in the normal Task Manager list. Very Very few people ever use scheduled tasks.
Chris Lively
Further, techs know to look in the event viewer when there is a problem. Scheduled tasks store that information in a log file on the file system. I'm willing to bet most people wouldn't even know where to look for that.
Chris Lively
+2  A: 

Why not provide both?

In the past I've put the 'core' bits in a library and wrapped a call to Whatever.GoGoGo() in both a service as well as a console app.

With something you're firing off every two minutes the odds are decent it's not doing much (e.g. just a "ping" type function). The wrappers shouldn't have to contain much more than a single method call and some logging.

+12  A: 

There's a well reasoned comparison here:

http://weblogs.asp.net/jgalloway/archive/2005/10/24/428303.aspx

Bottom line: scheduled tasks are usually preferred to Windows services.

Mark Ransom
At a run of once every 2 minutes you are getting dangerously close to the minimum that the scheduler can handle. Anything less than once a minute has to be handled in a service. Further, a scheduled task has a dependency on the scheduler itself. Finally, if your app has any external interaction (i.e.: waiting for data...) then you run into the possibility of it not being around when that data shows up.
Chris Lively
Bottom line: The decision is very dependent on the requirements of the application.
Chris Lively
+1  A: 

I'm really not a fan of Windows Scheduler. The user's password must be provided as @moodforall points out above, which is fun when someone changes that user's password.

The other major annoyance with Windows Scheduler is that it runs interactively and not as a background process. When 15 MS-DOS windows pop up every 20 minutes during an RDP session, you'll kick yourself that didn't install them as Windows Services instead.

Whatever you choose I certainly recommend you separate out your processing code into a different component from the console app or Windows Service. Then you have the choice, either to call the worker process from a console application and hook it into Windows Scheduler, or use a Windows Service.

You'll find that scheduling a Windows Service isn't fun. A fairly common scenario is that you have a long running process that you want to run periodically. But, if you are processing a queue, then you really don't want two instances of the same worker processing the same queue. So you need to manage the timer, to make sure if your long running process has run longer than the assigned timer interval, it doesn't kick off again until the existing process has finished.

After you have written all of that, you think, why didn't I just use Thread.Sleep? That allows me to let the current thread keep running until it has finished and then the pause interval kicks in, thread goes to sleep and kicks off again after the required time. Neat!

Then you then read all the advice on the internet with lots of experts telling you how it is really bad programming practice:

http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx

So you'll scratch your head and think to yourself, WTF, Undo Pending Checkouts -> Yes, I'm sure -> Undo all today's work..... damn, damn, damn....

However, I do like this pattern, even if everyone thinks it is crap: http://en.csharp-online.net/Creating_a_.NET_Windows_Service%E2%80%94Alternative_1:_Use_a_Separate_Thread

I've been running lots of Windows Services like this for years and it works for me. I still haven't seen a recommended pattern that people agree on. Just do what works for you.

Junto
Really you might want to run your service with its own service account. If everything runs a SERVICE or NETWORKSERVICE you are granting permissions to your app that it probably doesn't need (and that can risk the security of not just the one server but your entire network.)
Matthew Whited
Indeed. I don't think I stated otherwise?
Junto
You kind of implied otherwise in your first paragraph.The user's password is required for services just the same as for the task scheduler if you're not using one of the built-in accounts.
A: 

Some misinformation here. Windows Scheduler is perfectly capable of running tasks in the background without windows popping up and with no password required. Run it under the NT AUTHORITY\SYSTEM account. Use this schtasks switch:

/ru SYSTEM

But yes, for accessing network resources, the best practice is a service account with a separate non-expiring password policy.

Amit Naidu