Mastering Windows ServicesThis is a
series of articles in which we will explore and understand the
architecture, working, installation, maintenance and issues of Windows
services.
![]() IntroductionI know there are a number of articles on the Internet that talk about Windows Services, but unfortunately I didn't find any of the articles which we can call a complete Guide to Windows services. This made me come up with this series of articles. In this series, we will explore and understand the architecture, working, installation, maintenance and issues of Windows services. Before starting this article, let me present the whole outline which we are going to follow:
What is a Windows Service?Windows services enable you to perform tasks that execute as different background processes. It executes in its own process space until a user stops it or computer shuts down. This kind of application does not have a user interface. It is installed in the registry as an executable object. Windows Service ArchitectureNow let’s have a look at the Windows service architecture. The Windows service architecture consists of three components: Service ApplicationAn application that consists of one or more services that provides the desired functionality. Service Controller ApplicationAn application that enables you to control the behavior of a service. Service Control ManagerA utility that enables you to control the services that are installed on a computer. NamespaceThe Types of Windows ServicesWindows services can be categorized on the basis of number of services running in process space. The service that uses a single process space is called States of Windows ServicesThe Windows services can be either in start, stop, paused and continue states. In addition, the services can be in pending state. A pending state indicates that a command was issued but not completed. Programming Model of Windows Service ApplicationsYou create a Windows service application by using the classes of the ServiceBaseYou override the methods in the ServiceProcessInstaller and ServiceInstallerThese classes enable you to define the process of installing and uninstalling your service application. ServiceControllerThe methods of this class enable you to manipulate the behavior of your service. You can use its methods to start, stop and control service. Creating Windows ServiceTo create a Windows service, you create a class that extends the ![]() public class DBWriter : System.ServiceProcess.ServiceBase
I am summarizing these methods as follows: OnStartYou override this method to specify the tasks your service performs when it starts. You can set your service to start when the computer on which it is installed reboots. To do this, you set the ![]() /// <summary>
OnpauseYou override this method to specify the tasks your service performs when it pauses. OnstopYou override this method to specify the tasks your service performs when it stops. When the stop event occurs, the service control manager checks the value of the ![]() /// <summary>
OnContinueYou override this method to specify how your service behaves when it restarts after pausing. OnShutDownYou override this method to specify the tasks your service performs when computer is running on shuts down. OnCustomCommandYou override this method when you want a service to accept custom commands. The method takes an integer parameter on the basis of which a particular action is taken. ![]() protected override void OnCustomCommand(int command)
OnPowerEventYou override this method to specify how your service performs when it receives a power management event such as low battery. In the next article, we will see how to Install and Uninstall Windows service, and Control Windows Service. We will also explore the security contexts of this type of application. History
LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) |
|