What is Windows Service?
Windows Service applications run for a long time and are mostly used in server environments therefore they are usually called long-running applications. Capability to create windows service is one of the powerful features of .net.
Windows Service applications do not have any user interface or they do not produce any visual output. Services can run in the background while a user is performing or executing any other task in the foreground. If any user messages are generated, they are written to the Windows Event Log.
When the computer is booted, all windows services start automatically. They do not require any user to be logged in, for executing the services. Some services may load before a user logs in to the system, so these services can’t depend on a user to be logged in to run. Most Windows Services needs to be in running state all the time. So the services can run under any user including any system. There can be various Windows Services that start at boot-up time and system maintains the list of these services.
Windows Services are controlled by the Service Control Manager that helps to start, stop or pause the windows service, as needed.
Windows services were previously known as NT Services as they were introduced as a part of Windows NT operating system. They are not available on Windows 9x and Windows Millennium Edition (Me). For using windows service, any of the operating systems in the NT generation, such as, Windows NT, Windows 2000 Professional, or Windows 2000 Server can be used. So, when Windows 2000 got introduced, the name of the NT Services was changed to Windows Services.
Examples of windows services include task scheduling, running message queues, file indexing, plug and play device detection etc.
In the source code, Windows Service extends the System.ServiceProcess.Service class. All Windows Services that are built in .NET need to extend this class. Visual studio includes the following methods by default, which are overridden by the service when it is created.
- Dispose - clean up any managed and unmanaged resources
- OnStart - control the service startup
- OnStop - control the service stoppage
How to create Windows Service?
- Select a new project from File menu.
- Expand “Visual Basic” tab and select “Windows”.
- Then select Windows Service in it and specify the name of the service.
- Then right click on the form and select Add Installer.
- Project Installer gets added.
- Select ServiceInstaller1, go to properties and set DisplayName, ServiceName and set StartType as Automatic.
- Then select ServiceProcessInstaller1 and set Account property as LocalSystem.