Windows Server 2008 R2 and Windows 7 introduce a new feature that is known as the trigger-start service. A Windows service can be registered as a trigger-start service by using the following methods:
Use the Sc.exe utility together with the Triggerinfo option
Use the ChangeServiceConfig2 API programmatically.
This article describes an All-In-One Code Framework sample that is available for download. This sample includes a step-by-step guide that shows how to programmatically create a trigger-start service in Windows 7.
Note This code sample that is described in this article is not meant for use in a production environment. It is provided only for illustration. This code sample is released under the terms of the Microsoft Public License (MS-PL).
To download this code sample, click the following link:
Services and background processes have great influence on the overall performance of the system. If you reduce the total number of services, you reduce the total power consumption and increase the overall stability of the system.
In Windows Server 2008 R2 and in Windows 7, the Service Control Manager is extended so that a service can be automatically started and stopped when a specific system event or a specific trigger occurs on the system. This mechanism is called a service trigger event. A service can register to start or to stop when a service trigger event occurs. This feature eliminates the need for services to start when the operating system starts. Therefore, this feature prevents a situation in which a service is started even though there is no work for the service to perform. For example, you can define a service trigger event that occurs when a device is enabled by a specified device interface class. Or, you can define a service trigger event that occurs when a particular firewall port is available. A service can also register for a custom service trigger event that is generated by an Event Tracing for Windows (ETW) provider.
This code sample shows how to create a trigger-start service that starts when a generic USB disk becomes available. The sample also shows how to create a trigger-start service that starts a service when the first IP address on the TCP/IP networking stack becomes available. The sample also shows how to create a trigger-stop service that stops a service when the last IP address on the TCP/IP networking stack becomes unavailable. These start and stop events are reported in the Application log.
Note For more information about how to create the sample application and how to deploy the sample application, see the Readme.txt file that is included in the download package.
This sample contains code samples in the following programming languages:
Visual Basic .NET
You must run this code sample on a Windows Server 2008 R2 or Windows 7-based computer.
Note Service trigger events are not supported in versions of Microsoft Windows earlier than Windows Server 2008 R2 and Windows 7.
This sample application was created by using Microsoft Visual Studio 2008 with Service Pack 1 installed.
To build the CppWin7TriggerStartService project, you must have Microsoft Windows SDK for Windows 7 and the .NET Framework 3.5 Service Pack 1 installed.
Windows 7, Service Trigger Events, Windows Service
What is All-In-One Code Framework?
All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Web site: http://cfx.codeplex.com
How to find more All-In-One Code Framework samples
Microsoft Visual Studio 2008 Academic Edition, Microsoft Visual Studio 2008 Professional Edition, Microsoft Visual Studio 2008 Standard Edition, Microsoft Visual Studio Team System 2008 Database Edition, Microsoft Visual Studio Team System 2008 Architecture Edition, Microsoft Visual Studio Team System 2008 Development Edition, Microsoft Visual Studio Team System 2008 Test Edition, Microsoft Visual Studio Team System 2008 Team Suite