Tutorials » Creating a Windows Service in VB.NET

Introduction

If you have ever wanted your application to run constantly in the background while the machine is busy doing something else, one way to do it was to run it continuously or at a regular interval with the use of windows scheduler. There is one big disadvantage to his approach; someone had to log into the system to start this application.

A solution to this is to create a Windows Service for Windows 2000/Windows NT and this is what this article will teach you to do.

What is a Windows Service (previously called NT Service)?

The core function of a Windows Service is to run an application in the background. One of the ways that it differs from an windows application in that a Windows Service starts before any user logs in to the system (if it has been setup to start at boot up), although it can also be setup in such a way that it requires user to start it manually.

A Windows Service also has its own process hence it runs very efficiently. Normally a Windows Service will not have a user interface for a simple reason it can be run even if no one is logged on to 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 all the services running on your computer by opening 'Control Panel', 'Administrative Tools' and clicking 'Services'.

Creating a Windows Service in VB.NET

Prior to VB.NET, creating a Windows Service was a lot of work, as you had to use some system level procedure, which was not very easy, but thanks to VB.NET this is become a lot simpler and we shall now learn how to create a Windows Service.
One thing we should know before we dive in is that Windows Services are not available in Windows 95,98 or ME; you need to have Windows NT or Windows 2000 to run services.

The advantage to using .NET is that the framework incorporates all the classes, which shall help us to create, install and control Windows Service. Open your Visual Studio .NET, create a new Windows Service Project, which we shall call MyService, and click OK.

.

Add the Timer control from the Toolbar in the Components tab. In the properties window of Timer1, change the Interval property to 10000 which is 10 seconds.

Source Code

Double click the Timer1 control to open code window for Timer1_Elapsed and type the following code (this code shall be execute every 10 seconds):

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 the following code in the OnStart procedure (this procedure is called when you start the service, which shall enable the timer):

Timer1.Enabled = True

Now type the following code in the OnStop procedure (this procedure is called when you stop the service, which shall disable the timer):

Timer1.Enabled = False

Our application is now ready but few things before we move ahead when we build this application the executable created is not an windows application and hence you cant just click and run it, it needs to be installed as an service but don't worry we don't have to do it manually VB.Net has a facility where we could add an installer to our program and then use a utility to install the service.

About the Author

Jayesh Jain is working as applications consultant for a health company in Auckland, New Zealand. He has several years of n-Tier development experience and is currently working with
Visual Basic.NET to develop interactive client solutions. He has a passion for Web development and in the spare time he likes to write articles. Contact him at: jainjayesh74@yahoo.com