Imports System.ServiceProcess
Public Class SnapshotService
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
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 SnapshotService()}
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 LocalEventLog As System.Diagnostics.EventLog
Public WithEvents LocalTimer As System.Timers.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.LocalTimer = New System.Timers.Timer()
Me.LocalEventLog = New System.Diagnostics.EventLog()
CType(Me.LocalTimer, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.LocalEventLog, System.ComponentModel.ISupportInitialize).BeginInit()
'
'LocalTimer
'
Me.LocalTimer.Interval = 100000000000
'
'SnapshotService
'
Me.ServiceName = "Snapshot Service"
CType(Me.LocalTimer, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.LocalEventLog, System.ComponentModel.ISupportInitialize).EndInit()
End Sub
#End Region
Public Verbose As Boolean
Private DataFile As String
Private DataSchema As String
Protected Overrides Sub OnStart(ByVal args() As String)
Windows.Forms.MessageBox.Show("Entered OnStart")
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
' Create the event log if it doesn't exist, and set the local
' event log to point to it
If Not EventLog.SourceExists("Snapshot") Then
EventLog.CreateEventSource("Snapshot", "Snapshot Log")
End If
LocalEventLog.Source = "Snapshot Service"
LocalEventLog.Log = "Snapshot"
LocalEventLog.WriteEntry("The Snapshot service is starting")
' Get the bits from the registry so we know where to start looking
DataFile = GetSetting("Snapshot", "", "DataFile", Nothing)
DataFile = GetSetting("Snapshot", "", "DataSchema", Nothing)
' Check that all the required bits and pieces are about
If DataFile = Nothing Then _
Message_Die("There is no service data. You must create a DataFile before Snapshot can run.")
If Not (System.IO.File.Exists(DataFile)) Then _
Message_Die("'" & DataFile & "' does not exist, you must create the file before snapshot can run.")
If DataSchema = Nothing Then Message_Die("There is no service data. You must create a DataSchema before Snapshot can run.")
If Not (System.IO.File.Exists(DataSchema)) Then _
Message_Die("'" & DataSchema & "' does not exist, you must create the file before snapshot can run.")
' Set the timer to fire on the next hour
Dim TimeTillHour As Integer = (60 - Minute(Now())) * 60000
LocalTimer.Interval = TimeTillHour
LocalTimer.Enabled = True
LocalEventLog.WriteEntry("The Snapshot service started successfully")
Windows.Forms.MessageBox.Show("Exiting OnStart")
End Sub
Protected Overrides Sub OnStop()
Windows.Forms.MessageBox.Show("Entered OnStop")
' Add code here to perform any tear-down necessary to stop your service.
LocalEventLog.WriteEntry("The Snapshot service was stoped")
Windows.Forms.MessageBox.Show("Exiting OnStop")
End Sub
Private Sub Message_Die(ByVal Message As String)
LocalEventLog.WriteEntry(Message, EventLogEntryType.Error)
Me.Dispose()
End Sub
End Class