Creating a Windows Service in VB.net

By: Steven Holzner Emailed: 1746 times Printed: 2434 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

You can create a new Windows service easily in Visual Basic—just use the File|New|Project menu item, select the Windows Service icon in the templates box, and click OK.

For example, we created a full working Windows service example, WindowsService, in the In Depth section of this chapter-see that section for the details. Here's the code for that Windows service, Service1.vb:

Imports System.ServiceProcess

Public Class Service1
    Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

    Public Sub New()
        MyBase.New()

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

        ' Add any initialization after the InitializeComponent() call
        If Not EventLog1.SourceExists("Source1") Then
            EventLog1.CreateEventSource("Source1", "NewLog1")
        End If
        EventLog1.Source = "Source1"

    End Sub

    'UserService overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' The main entry point for the process
    <MTAThread()> _
    Shared Sub Main()
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT Service may run within the same process. To add
        ' another service to this process, change the following line to
        ' create a second service object. For example,
        '
        ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
        ' Service1, New MySecondUserService}
        '
        ServicesToRun = New System.ServiceProcess.ServiceBase () _
            {New Service1}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.
    ' Do not modify it using the code editor.
    Friend WithEvents EventLog1 As System.Diagnostics.EventLog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub _
        InitializeComponent()
        Me.EventLog1 = New System.Diagnostics.EventLog()
        CType(Me.EventLog1, _
            System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'EventLog1
        '
        Me.EventLog1.Log = "NewLog1"
        Me.EventLog1.Source = "Source1"
        '
        'Service1
        '
        Me.ServiceName = "Service1"
        CType(Me.EventLog1, _
            System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

    Protected Overrides Sub OnStart(ByVal args() As String)
        EventLog1.WriteEntry("Starting...")
    End Sub

    Protected Overrides Sub OnStop()
        EventLog1.WriteEntry("Stopping...")
    End Sub

End Class

VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

help

View Tutorial          By: sne at 2012-08-10 07:13:28

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Your first VB.NET Crystal Reports - A step by step guide
Data types in VB.net
For Loop in VB.net
How to export from DataGridView to excel using VB.net
The Option and Imports Statements in VB .NET
Read Data from Excel using OLEDB in VB.NET 2005
Insert cell data in an Excel file using OLEDB in VB.net
How to export from database to excel using VB.net
Chat Server in VB.net
Do Loop in VB.net
Getting an Exception's Number and Description in VB.net
Using Select Case in VB.net
A tutorial on Chat Server and Chat Client in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Most Emailed Articles (in VB.net)
OleDbConnection class in VB.net
String Insert, index off & format in VB.net
Multi Threaded Server Socket Programming in VB.net
The Option and Imports Statements in VB .NET
Data types in VB.net
Arrays and Dynamic Arrays in VB.net
Handling Strings in VB.net
Do Loop in VB.net
Using Excel 2007 Chart VB.net Picture Box
How to export from DataGridView to excel using VB.net
How to create an XML file in VB.NET using Dataset
Properties in VB.net
Nested If and Single line if statement
Stack data structure in VB.net
String equals, Copy to & Copy to in VB.net