views:

1447

answers:

5

I have only created regular windows applications (C# mostly). What differentiates a windows service from a regular windows application? What makes them different? What can a service do that an application can't? What are the differences seen from a developers point of view? How do you create one? Is it just to create a regular application (Console application maybe, since there are no gui?) and run or install it in a special way, or is it more that has to be done?

A: 

The main difference is that a windows service is something you want to run as a background service and doesn't require a UI. An example is a service that indexes files on your drive for searching.

Another benefit is you can have services automatically start when the user logs in.

There are also methods you can override that are called when the service is started/stopped (ie. from Control Panel | Administrative Tools | Services).

Within Visual Studio there is a special project type you can use to create it. See the site below for an example: http://www.dotheweb.net/articles/dotnet/services.aspx

soundslike
I'd rather say that you can have services running without any interactive sessions on the machine. You can start lots or programs when the user logs on in various ways. Using a service isn't really a benefit there.
Joey
+2  A: 

There are a couple of things that jump out to me immediately.

  • They run in an entirely different console starting with Vista
  • As a result of running in a different console, services cannot interact with the desktop. So essentially there is no direct UI support. You typically have to code a sibling UI application that does run as a normal program and uses some mechanism (named pipes for example) to communicate with the service.
  • Typically only one instance of your service can be running at any given time.
  • Processes are per user, services are per work station and hence often provide services for multiple users.
JaredPar
A: 

To quote MSDN, services "run in their own Windows sessions [...] can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface." -- all of these being different from normal applications. As a developer, creating a Service means [steps not mandatory or not meaningful for normal applications] that "you must create installation componentss" to "install and register the service on the server and create an entry for your service with the Windows Services Control Manager"; "Main [...] must issue the Run command"; etc, etc -- no dialog boxes, and so forth.

Alex Martelli
A: 

This MSDN page leads to more documentation on creating them than you could shake a stick at. This page is perhaps a better introduction to them in general.

The key difference between a process running as an app versus as a service is that the service can operate entirely outside the normal association with a user and session. Thus services can run such that they start before any user logs in and can continue running after users log off. Services are thus used to implement a great deal of the actual functionality of the operating system.

Services are also not tied to running as a 1:1 mapping with a process. Many services can exist within one process, normally through the use of svchost (take a look at these with process explorer for an indication of how this often works). This reduces the effort at startup as multiple processes are not required for relatively lightweight services.

Implementing a service in c# is pretty simple, this page indicates how in very easy to follow terms.

Note that in reality a service in windows is little more that the scaffolding in the registry under HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services which defines those 'image paths' (in most cases simply executables and the parameters to use) which are considered services along with which user then run as, which other services they depend on and whether they start at start up/post start up or as required.

ShuggyCoUk
+1  A: 

If you're familiar with Unix, a Windows service is like a Unix daemon. It isn't associated with any particular user, and is always running in the background.

quanticle