MSDN Magazine Launch Issue - February 15, 2008 - (Page 119) Windows Services Enhancements KENNY KERR T he state of Windows® service development was largely unchanged since the dawn of services in Windows NT®, but Windows Vista® and Windows Server® 2008 bring some significant changes to services. Many of these features are focused on making it simpler to produce services that are more secure, but among the nonsecurity-related service features are a few aimed at improving the overall responsiveness and reliability of Windows. Delaying Auto-Start Services Services can be configured to start only when they are specifically requested to do so via the StartService function. Such a service is called a demand-start service. Alternatively, they can be configured to start automatically during the operating system’s startup process. These are known as auto-start services, and this mode makes sense for a lot of services whose functionality should be available at all times or that may be required by other services. Some auto-start services, however, don’t absolutely need to be running at the time the user logs on to the computer because they may not be used immediately. Windows Vista takes another step toward reducing the Windows startup time by providing a facility for auto-start services to be started only after Windows has finished its own startup process. Specifically, a delayed auto-start service is activated roughly two minutes after regular auto-start services. The Service Control Manager (SCM) also creates the main service thread with a lower priority, ensuring that any logged on user isn’t notably impacted by delayed auto-start services. The priority is set back to normal after the service updates its state indicating that it is running. You can force a delayed auto-start service to start sooner by using the StartService function if it turns out that it hasn’t started by the time your application needs to make use of it. The following function shows how you can control this option (it should be called by your service’s setup program): bool ChangeDelayedAutoStart( SC_HANDLE service, bool delayed) { ASSERT(0 != service); SERVICE_DELAYED_AUTO_START_INFO info = { delayed }; return 0 != ::ChangeServiceConfig2( service, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &info); The service handle identifies the service to configure. This handle can be obtained by calling the OpenService or CreateService functions. The ChangeServiceConfig2 function lets you change a number of more advanced service configuration options. In this case, I’m using the SERVICE_CONFIG_DELAYED_AUTO_START_INFO flag to tell the function to expect a SERVICE_DELAYED_AUTO_ START_INFO pointer as its third parameter. ChangeServiceConfig2 returns zero if it fails, and you can get extended error information by calling the GetLastError function. Improving Shutdown Predictability } Prior to Windows Vista, it wasn’t always possible to ensure that services were stopped gracefully when the computer was shutting down. When the system notified the SCM that it was shutting down, it had only about 20 seconds to instruct all the running services to stop. If it took too long, the system would eventually simply terminate the SCM process. This could cause a numPrior to Windows Vista, ber of problems includit wasn’t always possible ing, ironically, increasing to ensure that services startup time since services that didn’t shut down gracewere stopped gracefully fully would often need to when the computer was deal with inconsistent state shutting down. when they were started up once again. Windows Vista introduced a new pre-shutdown notification that services can ask to receive. If you are developing a service that must shutdown gracefully and cannot necessarily do so quickly, then you can ask the SCM for a pre-shutdown notification. The SCM will wait (potentially indefinitely) for all of the pre-shutdown services to stop before proceeding with its traditional services shutdown process. Although this is good for services, it’s not necessarily that great for users who may want their computers to shut down promptly. For this reason, you should limit the use of this feature to very critical services and, preferably, only in server scenarios. A service indicates its desire to receive a pre-shutdown notification by including the SERVICE_ACCEPT_PRESHUTDOWN flag in its service status. The service’s handler function will then be notified via the SERVICE_CONTROL_PRESHUTDOWN control code. Although Windows is willing to wait indefinitely for a pre-shutdown service to stop, the service is required to remain relaunch2008 119
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.