Create a Windows Service project in Visual Studio 2005 as
File >> New >> Project
It will open the New Project window. Choose the following
Project types: Visual Basic >> Windows
Visual Studio installed templates: Windows
Specify the location for the solution and let the solution
name be AuthorLogService. Select the check box for "create
directory for solution."
Figure 1 – Creating new windows service project using Visual
Studio 2005 IDE
Click OK to create the solution and project. The IDE will
automatically add a Service1.vb class file in the
project AuthorLogService; rename Service1.vb
into AuthorLog.vb. Switch to the code view of AuthorLog.vb and open the OnStart
function of the AuthorLog class. Add the following
codes in the OnStart function.
Listing 1 – Function OnStart
in class AuthorLog.vb project
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Const iTIME_INTERVAL As Integer = 60000 ' 60 seconds.
Dim oTimer As System.Threading.Timer
"AuthorLogService has been started at " & Now.ToString())
Dim tDelegate As Threading.TimerCallback = AddressOf EventAction
oTimer = New System.Threading.Timer(tDelegate, Me, 0, iTIME_INTERVAL)
Next, implement EventAction procedure in the AuthorLog class. Note
that the EventAction is the procedure which is to be
fired/called at each timer interval of 60 seconds raised by Timer implemented
in OnStart function above. For a simple example, we
will implement to write the current time in a text file AuthorLog.txt in C
drive. Add the following code in the AuthorLog class
for EventAction procedure.
Listing 2 – Implementation of sample procedure EventAction in class AuthorLog.vb
Public Sub EventAction(ByVal sender As Object)
"AuthorLogService fires EventAction at " & Now.ToString())
Now we need to add installer
components in the service project to set service setup properties. Installer
components provided in Visual Studio automatically create Operating System
environment objects like registry keys, Windows services control manager event
handlers, executables for installation utilities when installing a service
application, SCM initialization, etc.
To add installer in the windows service
Open the AuthorLog.vb file in
design view. Right click on the form and choose Add Installer in the pop up menu.
Figure 2 – Add Installer in windows service project
This will add a new file ProjectInstaller.vb
with two controls, ServiceProcessInstaller1 and ServiceInstaller1, in the project. Select ServiceProcessInstaller1 and press F4 to open its
properties window. Set the Account type to LocalSystem.
Figure 3 – Setting Account type for the windows
Now select the ServiceInstaller1
control and press F4 to go to its properties window. Set the ServiceName as "AuthorLogService"
and StartType as "Automatic." Setting StartType to automatic will automatically start the service
(after installation) whenever the computer restarts.
Figure 4 – Setting service name and start type
using service installer component
Build the AuthorLogService
project. If the build is successful then we are ready with the service. Now to
install this service in the system we need an installer/setup file. So we will
create and add a setup project for the service in the
current solution through which we can install/uninstall the service.