Developing a Windows Service, Part II: Create a simple Windows Service
Programming, Windows March 17th, 2008Introduction
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
- Open Microsoft Visual Studio 2005

- 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.
- 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.

- On MyService.vb, right click on any empty space and select ‘Add Installer’.

- The ProjectInstaller is created. This file is for install the service. There are 2 components: ServiceProcessInstaller1 and ServiceInstaller1.

- 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.

- 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.

- Click on MyService.vb [Design], select ‘click here to switch to code view’ to edit code on this service.

- 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.

- 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
t1.Start() - OnStop method, abort the thread and write message to eventlog that the service is stopped.
t1.Abort()
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)
System.Threading.Thread.Sleep(10000)
End While
End Sub
- Add a global variable as a thread object.
- Change the startup object to MyService since you have rename it by right click on the project’s name and select Properties.

- Select ‘MyService’ on startup object.

- Save the project and build this project by right click on the project’s name and select build.

- If there are no errors, you have done creating a simple Windows Service now.

In the next part, I’ll show how to create a setup wizard (installer) for this service.
Related post
- Developing a Windows Service, Part III: Create a setup wizard Introduction In this part, I’m going to create an installer project for install my windows service ‘MyService’. The installer wizard...
- Developing a Windows Service, Part IV: Debug the service Introduction From previous part, you’ve learned to develop a simple windows service on Microsoft Visual Studio 2005 and how to...
- Developing a Windows Service, Part I: Introduction Introduction What is Windows service? Windows Service is an application or a program that can run without needing any user...
Related posts:






March 29th, 2008 at 8:58 pm
Is there a good ebook on this subject?
April 8th, 2008 at 5:12 pm
There should be. But if you search the internet about this topic, you’ll find lot of website talk about this which should be sufficient.
July 31st, 2009 at 10:40 pm
Briliant..Tnx a lot..