views:

1382

answers:

5

I just read this: http://stackoverflow.com/questions/442264/what-is-the-benefit-of-developing-the-application-as-a-windows-service but I'm still unsure of when to use a windows service.

I have a couple tasks that need to be run at intervals (i.e. every 5 minutes). Which project type should I use? Are there any examples of the types of applications that should be Windows services?

Thanks,

Kyle

+1  A: 

That is a typical case for windows service, IMO.

Otávio Décio
+10  A: 

For any scheduled task, I would usually recommend a Windows Service, for the following reasons:

  • A Windows Service will run even if a user is not logged into the PC (will run even if the server is sitting at the login prompt) (***Note - this may depend on the version of Windows you are running).
  • A service can run as high-authority accounts such as Network Service or Local System, or a User - they have more configurability in that regard
  • A service also comes built in with options for starting, stopping, restarting, and pausing while running (sometimes)
  • You can also set up failure conditions for services, like if it fails have it auto-restart

As far as other examples of applications that can be windows services, a lot of times they are useful for applications such as remoting - you can have a service run a remoting server that clients connect to. Obviously very useful for data processing tasks that you want to run in the background as well, or processes where you want to send an email on certain conditions, etc.

In general I've always found scheduled tasks to be much more fragile and unreliable. And unless you have them log properly, often harder to debug.

In reference to the bug with the Timer - if you read the bug report on MS's site, you can see that it is caused when you call "Stop" inside the Timer_Elapsed event. The answer to this is simple - don't call stop. Instead, wrap the whole thing in a check for a "IsRunning" boolean and only run if IsRunning is false. Even if there wasn't an issue with the timer, you need to do this anyway because the timer might re-fire during your execution if your execution takes longer than your timer interval.

Anyway, I still think using scheduled tasks is a weak solution and gives me flashbacks of Windows 95.

Sam Schutte
A scheduled task does not need a logged on user. A scheduled task can run as a built-in account. For these types of maintenance tasks controllability and failure actions are pretty much irrelevant. Your examples of service programs are very good.
Stephen Martin
I'm not sure where people have got the idea that the Task Scheduler service is somehow unreliable, this was perhaps true in Windows 98 but certainly post Win2K this is categorically not true.
Stephen Martin
Added correction re: logged in user - for maintenance tasks, restarting can be useful. For instance if you have a 3rd party tool that you're using, and due to things outside of your control, it crashes sometimes. When this happens, the service can reboot the server (just a for instance).
Sam Schutte
In regards to your second comment - I guess I've just been burned too many times by it, even in 2000 and XP. I'd much rather have a windows service in all cases - but that's just me.
Sam Schutte
A user has never needed to be logged on for NT based systems, only Win9x based.
Stephen Martin
A: 

I would prefer windows service in most cases.
One good thing when using scheduled tasks:
All used resources are released when the scheduled task have finished.

When using windows service (without stopping the service), the process never dies. And you must in your program make sure that resources are released.

Kb
When stopping a windows service, the resources are released also.
StingyJack
@StringyJack: Yes, I agree. When you stop the service. But if you do not stop the windows service... ;))
Kb
Hey... My Developer Gut is offended at the "Stringy" remark. =)
StingyJack
+3  A: 

For single or narrow purpose applications run on a schedule, running a console application via the Task Scheduler is almost always the correct design. For long running tasks and complex tasks that may need interaction such as manual start, stop, pausing, continuing, etc. then, in general, a Windows Service is the better option. Though with the new Task scheduler in Vista and later the balance is changing more toward scheduled tasks.

Scheduled tasks can be run under any account and do not need a user logged in just like Services. For single purpose tasks such as those you propose external control of the task is usually irrelevant so you have no need of the controllability of a Service.

A big factor also is the Task Scheduler is a very robust and flexible event based scheduler. It is extremely unlikely that you could write a scheduler that was more robust and could handle the vagaries of time and trigger based scheduling. In fact there are a number of questions about using timers for scheduling tasks in services on this site and it is remarkable the number of answers (including some of the 'correct' answers) that are poor quality or outright incorrect.

EDIT: It's also interesting to note that Microsoft policy is shifting away from using services for task based actions. If you check Vista, Win2K8 and Win7 you will notice a growing list of special purpose scheduled tasks that perform system maintenance and many system services.

Stephen Martin
+1  A: 

I have a number of windows scheduled tasks that run hourly on a production web server. They are not reliable at all. They are running in Windows 2003 Server under a specific machine account. Most of the time they work perfectly, but occasionally they fail to run and sometimes they terminate before they are finished.

Some of this may be due to the fact that they are vbscripts and the way they are written, but I've seen scheduled tasks with WS FTP Pro (commercial FTP software) that behave the same way.

I've converted many of these to windows services and have never had to worry about them again.

I would definitely lean towards windows services. Like some of the other comments, I have been burned by windows scheduled tasks too many times. I don't trust them for enterprise level solutions.

Frank