Click here to Skip to main content
Click here to Skip to main content

Scheduling tasks with VB.NET as Windows services

, 5 May 2005
Rate this:
Please Sign up or sign in to vote.
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.

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.

''
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

Share

About the Author

shan
Web Developer
China China
No Biography provided

Comments and Discussions

 
GeneralGood article PinmemberFernando Tripaldi7-Nov-10 3:07 
Questionwindow service Pinmemberkk_upadhyay22-Jul-07 21:57 
GeneralGreat Article PinmemberMember #372783721-Mar-07 10:41 
Generaleasy to implement Pinmemberbayla13-Nov-06 7:54 
Generalstruggling with remoting of a form Pinmemberdpietro23-May-06 6:31 
Generaluser interface in windows service PinmemberSebastián Vázquez19-Apr-06 7:19 
GeneralRe: user interface in windows service PinmemberDarka19-Apr-06 7:38 
GeneralIntegrate window service with any web application PinmemberFaheem Habib25-Aug-05 1:37 
GeneralRe: Integrate window service with any web application PinmemberDigiOz Multimedia25-Aug-07 12:41 
Questionc# implementation? PinmemberUnruled Boy5-May-05 4:22 
AnswerRe: c# implementation? PinmemberShan Natarajan5-May-05 4:26 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 5 May 2005
Article Copyright 2005 by shan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid