Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a windows service and a regular application?

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?

like image 595
Svish Avatar asked Aug 01 '09 22:08

Svish


People also ask

What is the difference between a service and an application?

An application is software that is designed to be installed and managed by users. A service is software that is managed for users. This includes services such as APIs that users never use directly. It also includes services such as a website that people use but don't install and manage for themselves.

What are Windows services and applications?

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.

What is meant by Windows service?

In Windows NT operating systems, a Windows service is a computer program that operates in the background. It is similar in concept to a Unix daemon. A Windows service must conform to the interface rules and protocols of the Service Control Manager, the component responsible for managing Windows services.

When should you use a Windows service?

You would typically use Windows services to implement long-running jobs that will be executed at predefined intervals of time. Your Windows service will continue to run in the background while the applications in your system can execute at the same time.


2 Answers

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.
like image 125
JaredPar Avatar answered Oct 05 '22 13:10

JaredPar


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.

like image 35
ShuggyCoUk Avatar answered Oct 05 '22 13:10

ShuggyCoUk