Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / Visual Basic
Article

Scheduling tasks with VB.NET as Windows services

Rate me:
Please Sign up or sign in to vote.
4.17/5 (17 votes)
5 May 20051 min read 156.9K   5.2K   80   11
An article on Windows services using VB.NET.

Introduction

After the introduction of .NET, writing services using VB.NET has become very simple and easy. But still beginners need a start from somewhere to know how to achieve this. Attached is a sample application which demonstrates this concept.

Background

This is a slightly modified version of the article submitted by Xiangyang Liu, to create a simple Windows service using VB.NET. Thanks to Xiangyang Liu, for the code that I have used to build Windows services in .NET. This code explains some additional things like, XML file reading, database and stored procedure handling etc.. I have simplified the code to make it simple to understand for the beginners.

Using the code

For those who know a bit of .NET applications, this code is pretty simple. To install and test this application, please modify the application path within the Installservice.bat which exists in the bin folder.

//
serviceInstaller -i NotificationService 
  D:\Training\WindowsService\Shanservice\ 
  NotificationService\bin\NotificationService.exe
ServiceInstaller -r NotificationService
//

If the path is correct in the installservice.bat, then the new service called NotificationService will be installed and started within the Service Control Manager.

Image 1

The class NotificationService is inherited from System.ServiceProcess.ServiceBase. It will run the code only once, when the user logs in for the first time or when the service is started. But if you want the action to keep happening depending upon a trigger you need to use the timer calls from the system. Please also note that, the Timer event calls the subroutine which sends an email. I have set it to fire the event every two seconds. You can change this if you want to. I have used my personal email ID here. Please change it before you test the code or I will be getting millions of emails.

VB
''
Protected Overrides Sub OnStart(ByVal args() As String)
        Try
            t = New Timer(2000)
            AddHandler t.Elapsed, AddressOf TimerFired
            With t
                .AutoReset = True
                .Enabled = True
                .Start()
            End With
        Catch obug As Exception
            LogEvent(obug.Message)
            Throw obug
        End Try
    End Sub

 Protected Overrides Sub OnStop()
  Try
  log.WriteEntry("Service Stopping", EventLogEntryType.Information)
  t.Stop()
  t.Dispose()
  Catch obug As Exception
  LogEvent(obug.Message)
  End Try
  End Sub

Private Sub TimerFired(ByVal sender As Object, ByVal e As ElapsedEventArgs)
  Working()
End Sub
''

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood article Pin
Fernando Tripaldi7-Nov-10 3:07
Fernando Tripaldi7-Nov-10 3:07 
Questionwindow service Pin
kk_upadhyay22-Jul-07 21:57
kk_upadhyay22-Jul-07 21:57 
GeneralGreat Article Pin
Member 372783721-Mar-07 10:41
Member 372783721-Mar-07 10:41 
Generaleasy to implement Pin
bayla13-Nov-06 7:54
bayla13-Nov-06 7:54 
Generalstruggling with remoting of a form Pin
dpietro23-May-06 6:31
dpietro23-May-06 6:31 
Generaluser interface in windows service Pin
Sebastián Vázquez19-Apr-06 7:19
Sebastián Vázquez19-Apr-06 7:19 
GeneralRe: user interface in windows service Pin
Jonathan [Darka]19-Apr-06 7:38
professionalJonathan [Darka]19-Apr-06 7:38 
GeneralIntegrate window service with any web application Pin
Faheem Habib25-Aug-05 1:37
Faheem Habib25-Aug-05 1:37 
GeneralRe: Integrate window service with any web application Pin
DigiOz Multimedia25-Aug-07 12:41
DigiOz Multimedia25-Aug-07 12:41 
Questionc# implementation? Pin
Huisheng Chen5-May-05 4:22
Huisheng Chen5-May-05 4:22 
AnswerRe: c# implementation? Pin
Member 10003095-May-05 4:26
Member 10003095-May-05 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.