Developing Windows Service on VB.NET, Part 2: Create Windows Service

This entry is part 2 of 4 in the series Developing Windows Service on VB.NET

In this part, I’ll show how to create a simple Windows Service project using VB.NET on Microsoft Visual Studio 2005. The example service will generate a message to application event log every 10 seconds.

Step-bystep to create Windows Service

  1. Open Microsoft Visual Studio 2005
    Open Microsoft Visual Studio 2005
  2. Create new Project.
    • On project type, select Visual Basic -> Windows.
    • On templates, select Windows Service.
    • Name the service. In this example, it is ‘MyService’.
    • Check create directory for solution and click OK.

    Create Windows Service - Create Windows Service project

  3. When the Windows service’s project is created, change the default name (service1) to ‘MyService’ on filename in solution explorer, (Name) and ServiceName in properties. You can customize service to auto generate message log to eventlog (AutoLog = True), can stop this service (CanStop = True), etc on this properties.
    Change name to 'MyService'
  4. On MyService.vb, right click on any empty space and select ‘Add Installer’.
    Add Installer
  5. The ProjectInstaller is created. This file is for install the service. There are 2 components: ServiceProcessInstaller1 and ServiceInstaller1.
    Installer's components
  6. Click on the first component (ServiceProcessInstaller1), you can customize account to run this service on the properties. The default account running this service is User. In this example, I change to LocalService.
    Change running account
  7. Click on the second component (ServiceInstaller1), you can change serviceName which appears in services and startup type. In this example, I change ServiceName to ‘MyService’ and leave startup type to manual.
    Change service name and service's startup type
  8. Click on MyService.vb [Design], select ‘click here to switch to code view’ to edit code on this service.
    Code view on MyService.vb
  9. On MyService.vb, there is a class called ‘MyService’ with 2 methods: OnStart and OnStop. OnStart method will be invoke when you start this service and OnStop method will be invoke when you stop this service.
    OnStart/OnStop method
  10. Add some code to the project. I’ll create a thread to generate message every 10 seconds. This thread will be started when the service start.
    • Add a global variable as a thread object.
      Private t1 As System.Threading.Thread
    • OnStart method, set the thread object on running method to run on background and start it.
      t1 = New System.Threading.Thread(AddressOf running)
      t1.IsBackground = True
    • OnStop method, abort the thread and write message to eventlog that the service is stopped.
      EventLog.WriteEntry(“My service is stopped”, EventLogEntryType.Information)
    • Create new method running that looping generate message to event log every 10 seconds.
      Public Sub running()
      While (True)
      EventLog.WriteEntry(“My service is running”, EventLogEntryType.Information)
      End While
      End Sub

    Coding on MyService

  11. Change the startup object to MyService since you have rename it by right click on the project’s name and select Properties.
    Open project's properties
  12. Select ‘MyService’ on startup object.
    Change startup object
  13. Save the project and build this project by right click on the project’s name and select build.
    Build the project
  14. If there are no errors, you have done creating a simple Windows Service now.
    Project's output

In the next part, I’ll show how to create a setup wizard (installer) for this service.

Series Navigation<< Developing Windows Service on VB.NET, Part 1: IntroductionDeveloping Windows Service on VB.NET, Part 3: Create Setup Wizard >>


  1. T1 March 29, 2008
  2. linglom April 8, 2008
  3. Tha July 31, 2009
  4. Chandra Sekhar January 30, 2011

Leave a Reply