Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Create a Windows Service in VB.NET

 
 

In this tutorial we will create an empty windows service, having followed a few of the tutorials found online, I thought I would make my own as the ones I followed had a few bits missing. So, open Visual Studio Pro and create a new Windows Service Project, call this project "Empty Service".

Windows Service in VB.NET

Once the project is open, right-click on Service1's design window and select "Add Installer":

Windows Service in VB.NET - Add Installer

The ProjectInstaller has 2 components:

ServiceProcessInstaller1 and ServiceInstaller1, both of which have properties that need configuring. Configure them as follows:

ServiceProcessInstaller1 - Account, this is the user account that the service will run under, set this to LocalSystem.

ServiceInstaller1 - Description, this is the description that will appear in the services admin tool. Set this to Empty Service Description

ServiceInstaller1 - Display Name, this is the service name that will appear in the services admin tool. Set this to Empty Service

ServiceInstaller1 - ServiceName, Set this to EmptyService.

ServiceInstaller1 - StartUpType, this determines whether the service will start automatically with windows, set this to Manual

 

Now, close the ProjectInstaller design window and go back to the service design window and add an Eventlog control from the Toolbox components. Call this Eventlog, evlMain. Also change it's log property to Application and the Source to the service name EmptyService.

Now right-click on the design window and select "view code". When the code window opens right-click on the Service1.vb in the solution explorer and rename it to EmptyService.vb.

Add the following code to the code window overwriting all existing code:

Public Class EmptyService
Inherits System.ServiceProcess.ServiceBase

Private timerThread As System.Threading.Thread
Private Timer1 As New System.Timers.Timer

Private Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Me.AutoLog = False ' We will write our log events ourself
Me.CanPauseAndContinue = True
AddHandler Timer1.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf TimerElapsed)

Timer1.Interval = 3000

End Sub

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

Timer1.Enabled = True
timerThread = New System.Threading.Thread(AddressOf Timer1.Start)
timerThread.Start()

'Add event log entry
evlMain.WriteEntry("Empty Service Started.")

End Sub

Protected Overrides Sub OnPause()
MyBase.OnPause()
'Add event log entry
evlMain.WriteEntry("Empty Service Paused.")
End Sub

Protected Overrides Sub OnContinue()
MyBase.OnContinue()
'Add event log entry
evlMain.WriteEntry("Empty Service Continuing.")
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.

Timer1.Enabled = False

'Add event log entry
evlMain.WriteEntry("Empty Service Stopped.")

End Sub

Private Sub TimerElapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)

' This sub runs every time the elapsed milli-seconds of the timer pass
' Sp add your code here.

End Sub

End Class

 

First we create a new thread called timerthread and a new timer called timer1, then we create a private sub called new in which we set AutoLog to false , because we want to write our own log events and then set CanPauseandContinue option to True, meaning our service can be paused and then unpaused, The New sub will run when the service class is created. The New sub also adds a new handler to the timer, this new handler is called elapsed and it means that once the preset number of ticks have elapsed in the timer, then the TimerElapsed sub will run. The tick interval is then set to 3000 milliseconds. The OnStart, OnPause, OnContinue and OnStop are self-expanitory, except for OnStart which creates a new thread and runs our timer on it. Lastly we have the TimerElapsed sub, this sub will run every 3 seconds and this is where you should out your code.

Right, thats it, now you cannot run a service as you would a normal program, but the .net framework has a tool that will allow you to install it first. The InstallUtil.exe program is located in the framework folder. From the command prompt type:

%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\EmtpyService.exe

where C:\EmtpyService.exe is the path to you service, to uninstall the service type:

%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u C:\EmtpyService.exe

Once your service is installer you can find it in the Services admin tool by typing Services.msc at the run prompt:

Windows Service in VB.NET - Service Installed

Please note that programs like windows defender will prompt you to allow the installation of any service that attempts to run using the local system account.

All Done.

Empty Service Source Code - 58Kb.

     
 
 
     

 

Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates
 
   
 
 

 

__PayPal

 
Please Donate to the Nvidia Geforce Go 7950 GTX Fund, All donations welcome. Thanks.