How to: Create Windows Services

When y'all create a service, you lot tin employ a Visual Studio project template called Windows Service. This template automatically does much of the work for you by referencing the appropriate classes and namespaces, setting upwardly the inheritance from the base class for services, and overriding several of the methods you're likely to want to override.

Warning

The Windows Services project template is not available in the Express edition of Visual Studio.

At a minimum, to create a functional service you lot must:

  • Set the ServiceName holding.

  • Create the necessary installers for your service awarding.

  • Override and specify lawmaking for the OnStart and OnStop methods to customize the means in which your service behaves.

To create a Windows Service application

  1. Create a Windows Service projection.

  2. In the Properties window, ready the ServiceName property for your service.

    Set the ServiceName property.

    Notation

    The value of the ServiceName belongings must ever lucifer the name recorded in the installer classes. If yous change this belongings, you must update the ServiceName belongings of installer classes besides.

  3. Ready any of the following properties to make up one's mind how your service volition role.

    Property Setting
    CanStop True to signal that the service will accept requests to stop running; false to preclude the service from being stopped.
    CanShutdown True to indicate that the service wants to receive notification when the reckoner on which it lives shuts downwards, enabling it to call the OnShutdown procedure.
    CanPauseAndContinue True to indicate that the service will accept requests to pause or to resume running; fake to prevent the service from being paused and resumed.
    CanHandlePowerEvent Truthful to betoken that the service can handle notification of changes to the reckoner'south power condition; faux to prevent the service from existence notified of these changes.
    AutoLog True to write informational entries to the Application event log when your service performs an action; false to disable this functionality. For more information, encounter How to: Log Data Most Services. Notation: By default, AutoLog is set up to true.

    Notation

    When CanStop or CanPauseAndContinue are fix to faux, the Service Control Manager will disable the corresponding carte options to cease, suspension, or continue the service.

  4. Admission the Code Editor and make full in the processing you desire for the OnStart and OnStop procedures.

  5. Override whatever other methods for which you lot desire to define functionality.

  6. Add the necessary installers for your service awarding. For more than data, come across How to: Add Installers to Your Service Application.

  7. Build your project past selecting Build Solution from the Build menu.

    Notation

    Do not press F5 to run your projection — you cannot run a service project in this way.

  8. Install the service. For more information, encounter How to: Install and Uninstall Services.

Run into besides

  • Introduction to Windows Service Applications
  • How to: Write Services Programmatically
  • How to: Add together Installers to Your Service Application
  • How to: Log Data About Services
  • How to: Start Services
  • How to: Specify the Security Context for Services
  • How to: Install and Uninstall Services
  • Walkthrough: Creating a Windows Service Application in the Component Designer