Creating a Windows Service in VB.NET
What is Windows Service
Previously called an NT service, the core function of a Windows aervice is to run an application in the background. There are few things that make them different from a Windows application. A Windows service starts much before any user logs in to the system (if it has been setup to start at boot up process). A Windows service can also be setup in such a way that it requires a user to start it manually – the ultimate customization!
Windows services have their own processes, and hence run very efficiently. Normally a Windows service will not have a user interface for the simple reason that it can be run even if no one is logged into the system, but this is not a rule — you can still have a Windows service with a user interface.
In windows 2000 you can view a list of services currently running on your computer by opening Control Panel -> Administrative Tools -> Services
Creating A Windows Service in VB.NET
Prior to VB.NET, creating a Windows service was a lot of work, and was left to the C++ guru’s, as you had to use some system level procedures, which were extremely difficult. Thanks to VB.NET, however, this is becoming very easy and we shall now learn how to create a Windows Service in VB.NET.
There are a few things that you should know before we dive in, however. Windows services are not available under Windows 95, 98 or ME — you need to have Windows NT or Windows 2000 to run services.
The advantage to use .NET is that the framework incorporates all of the classes, which shall help us to create, install and control a Windows Service. Open Visual Studio .NET and create a new Windows service project, which we shall call “MyService”. Click OK.
Add a timer control from the toolbar in the Components tab (not the Windows Forms tab!). In the properties window of Timer1, change the interval property to 10000, which is 10 seconds.
Examining The Source Code
Double click the timer1 control to open up the code window for Timer1_Elapsed. Type in the following code:
Dim MyLog As New EventLog() ' create a new event log
' Check if the the Event Log Exists
If Not MyLog.SourceExists("MyService") Then
MyLog.CreateEventSource("MyService", "Myservice Log") ' Create Log
End If
MyLog.Source = "MyService"
' Write to the Log
MyLog.WriteEntry("MyService Log", "This is log on " & _
CStr(TimeOfDay), EventLogEntryType.Information)
Type in the following code for the OnStart procedure:
Timer1.Enabled = True
Type in the following code in the OnStop procedure:
Timer1.Enabled = False
Our application is now ready, but there are a few things that we need to do before we move ahead when we build this application. The executable created is not a Windows application, and hence you can’t just click and run it — it needs to be installed as a service, but don’t worry, we don’t have to do it manually — VB.Net has a facility where we can add an installer to our program and then use a utility to install the service.
Adding an Installer to the Project
Open the service1.vb design window, right click on it and select Add Installer option, which will add an installer project (called ProjectInstaller.vb) with two controls — ServiceProcessInstaller1 and ServiceInstaller1 — to our existing project.
Select the ServiceInstaller1 control and open the property window. Change the ServiceName property and DisplayName property to MyService (this is the name you want to appear in the list of services in the services window). Select the ServiceProcessInstaller1 control and open the property window. Change the Account property to LocalSystem (this needs to be specified as we need to run the service on our local machine).
Now it’s time for us to build the application and create an executable. Select Build Solution from the Build menu to create an executable with installation instructions for the service.
Installing the Service
To install our service we need to use the InstallUtil program, which is a .NET utility to install Windows services. You can find it in C:\WINNT\Microsoft.NET\Framework\v1.0.3705
Note: This directory might be different on your computer, depending upon the version of the .NET framework you are working with Alternatively, you could run a .NET Command Window by selecting Start –> Programs -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools >- Visual Studio .NET Command Prompt, which sets all of the required paths for you.
Type the following command in the command window:
InstallUtil "d:\My documents\development\applications\MyService\bin\Myservice.exe"
This is the path for the executable of the service we just created. Remember that VB.NET created the executable in the Bin folder under the project folder, so make sure you change this to your executable path.
Starting the service
Running a service and starting a service are two different things — when you install the service with InstallUtil you are running the service, but have yet have to start it.
To view and start the service, open Control Panel -> Administrative Tools. Now click Services, locate MyService, right click on it and select Start to start it:
Our service is now started. Open the Event Viewer from Administrative Tools and click Application Log to see the logs created by the Service (MyService) every 10 seconds. If you don’t see any logs click refresh (F5). You will have to keep refreshing to see the latest event logs:

Stopping the Service
This procedure is similar to installing the service but now we shall run the InstallUtil with the /U parameter, which will uninstall the service:
InstallUtil /U "d:\My documents\development\applications\MyService\bin\Myservice.exe"
(or the executable path on your computer).
Take note of the message to confirm that the service was uninstalled properly.
Tips
- Stop the service and close the service window before you install/uninstall the service.
- Always uninstall/install if you make any changes to the service application.
- Try avoiding a user interface, inputs and message boxes in the service application.
- Open ProjectInstaller.vb, select the ServiceInstaller1 control, and open the property window. Change the StartType property to automatic if you want to start the service automatically.
- If you want to debug your windows service, it isn’t quite as straight forward as usual. Start your service, then go to Debug|Processes in the Visual Studio IDE. Find MyService.exe (you may need to check a box to display system processes), and attach the debugger. Then you’ll be able to set breakpoints as usual – but not in the Start procedure
Conclusion
Windows services are often overlooked. You should use a Windows service instead of a standard application when you need to monitor or administer something in the background. The only disadvantage of using a service is the installation procedure, but I bet that there are more positive aspects then negative when selecting a Windows service over an application running with Windows Scheduler!








