p>As part of DotNetDoc's new "Ask the Doc" format, we are starting our journey by taking a page from the Zen Master himself Scott Hanselman. We will be walking through his list of questions posted in his, "What Great .NET Developers Ought To Know" post. We will not only try to answer the questions but give each a "deep dive" so that we not only can answer the question, but so that we UNDERSTAND the answer. We will go one question at a time until we reach the end. I hope you enjoy, and thanks Scott for the thought provoking questions.
.Net Ought To Know #2 : What is a Windows Service and how does it's lifecycle differ from a standard EXE?
Whenever a user logs onto a NT flavor machine, they are given a desktop to work with. Any of the programs that are run, any of the processes that are started belong to this user. Only one user can be logged on at one time.
If you go to windows explorer and navigate to the Documents and Settings folder you will see all of the different users that have used your machine. Only one of these profile (or desktops) can be active at a time. Different rights can be given to each of the users of the system which can control what they can see and what they can do. Standard EXE's are run by users that are logged onto the system. This is accomplished by either navigating to the shortcut associated with the application (either on the programs menu or the desktop) and clicking on the icon. This will run that application using the rights of the currently logged on user. You can also run as another user by right-clicking on the icon and selecting run-as from the context menu, but you get the picture. Standard EXE's are associated with the current user and their desktop.
Windows Services, on the other hand, are processes that can exist and run without a user/desktop. The windows service runs in its own windows session without the need for any particular user to be logged onto the system. They are initiated when the system boots up. As opposed to a Standard EXE, they have no user interface as they run in the background of your computer.
You can view all the Windows Services running on your computer by using the Computer Management Console. This can be opened by clicking on the services icon found in your control panel. Many of the applications found in here will be familiar to you like SQL Server (MSSQLSERVER for 2000 or SQL Server * for 2005) or Task Scheduler, but others will not be, like Network DDE or Logical Disk Manager.
To discover what a Windows Service is used for, just click on the service and the description will be shown on the left of the screen. Finally, you can also decide when a service will start. You have three choices :
- Automatic -
When this setting is selected, the Windows Service will start when the system is started.
- Manual -
When this setting is selected, the Windows Service will not be started until the "Start the Service" link is selected. This can also be automated by an application but the main difference is that it is not started during the system boot up.
- Disabled -
Well, this of course means that the service is disabled.
So the lifecycle of a Windows Service is different from the lifecycle of a Standard EXE because a Windows Service is associated with the system (but can use any account) and begins its "life" when the system boots up. A Standard EXE needs a signed on user(manually or programmaticlly)to initiate its instantiation.