Took me a while to find a good example of what is needed, but after googling for "ServiceBase" i hit gold. Here's the link to the MSDN docs (now why didn't I think to look there first?)
Windows Service Base Implementation
And here's the example:
' Turn on the constant for trace output.
#Const TRACE=True
Imports System
Imports System.ComponentModel
Imports System.IO
Imports System.ServiceProcess
Imports System.Threading
Imports System.Diagnostics
Namespace ServiceSample
' Define custom commands for the SimpleService.
Public Enum SimpleServiceCustomCommands
StopWorker=128
RestartWorker=129
CheckWorker = 130
End Enum
' Define a simple service implementation.
Public Class SimpleService
Inherits System.ServiceProcess.ServiceBase
Private Const logFile = "C:\service_log.txt"
Private Shared Dim serviceTraceListener As TextWriterTraceListener = Nothing
Private Dim workerThread As Thread = Nothing
Public Sub New()
CanPauseAndContinue = true
ServiceName = "SimpleService"
End Sub
Public Shared Sub Main()
' Create a log file for trace output.
' A new file is created each time. If a
' previous log file exists, it is overwritten.
Dim myFile As StreamWriter = File.CreateText(logFile)
' Create a new trace listener that writes to the text file,
' and add it to the collection of trace listeners.
serviceTraceListener = New TextWriterTraceListener(myFile)
Trace.Listeners.Add(serviceTraceListener)
Trace.AutoFlush = True
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Service main method starting...", _
"Main")
' Load the service into memory.
System.ServiceProcess.ServiceBase.Run(New SimpleService())
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Service main method exiting...", _
"Main")
' Remove and close the trace listener for this service.
Trace.Listeners.Remove(serviceTraceListener)
serviceTraceListener.Close()
serviceTraceListener = Nothing
myFile.Close()
End Sub
Private Sub InitializeComponent()
' Initialize the operating properties for the service.
Me.CanPauseAndContinue = True
Me.CanShutdown = True
Me.ServiceName = "SimpleService"
End Sub
' Start the service.
Protected Overrides Sub OnStart(ByVal args As String())
' Start a separate thread that does the actual work.
If (workerThread Is Nothing) OrElse _
((workerThread.ThreadState And _
(System.Threading.ThreadState.Unstarted Or System.Threading.ThreadState.Stopped)) <> 0)
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Starting the service worker thread.", _
"OnStart")
workerThread = New Thread(New ThreadStart(AddressOf ServiceWorkerMethod))
workerThread.Start()
End If
If Not workerThread Is Nothing
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Worker thread state = " + _
workerThread.ThreadState.ToString(), _
"OnStart")
End If
End Sub
' Stop this service.
Protected Overrides Sub OnStop()
' Signal the worker thread to exit.
If (Not workerThread Is Nothing) AndAlso workerThread.IsAlive
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Stopping the service worker thread.", _
"OnStop")
workerThread.Abort()
' Wait up to 500 milliseconds for the thread to terminate.
workerThread.Join(500)
End If
If Not workerThread Is Nothing
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Worker thread state = " + _
workerThread.ThreadState.ToString(), _
"OnStop")
End If
End Sub
' Pause the service.
Protected Overrides Sub OnPause()
' Pause the worker thread.
If (Not workerThread Is Nothing) AndAlso workerThread.IsAlive _
AndAlso (workerThread.ThreadState And _
(System.Threading.ThreadState.Suspended Or System.Threading.ThreadState.SuspendRequested)) = 0
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Suspending the service worker thread.", _
"OnPause")
workerThread.Suspend()
End If
If Not workerThread Is Nothing
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Worker thread state = " + _
workerThread.ThreadState.ToString(), _
"OnPause")
End If
End Sub
' Continue a paused service.
Protected Overrides Sub OnContinue()
' Signal the worker thread to continue.
If (Not workerThread Is Nothing) AndAlso workerThread.IsAlive _
AndAlso (workerThread.ThreadState And _
(System.Threading.ThreadState.Suspended Or System.Threading.ThreadState.SuspendRequested)) <> 0
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Resuming the service worker thread.", _
"OnContinue")
workerThread.Resume()
End If
If Not workerThread Is Nothing
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Worker thread state = " + _
workerThread.ThreadState.ToString(), _
"OnContinue")
End If
End Sub
' Handle a custom command.
Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Custom command received: " + _
command.ToString(), _
"OnCustomCommand")
' If the custom command is recognized,
' signal the worker thread appropriately.
Select command
Case SimpleServiceCustomCommands.StopWorker
' Signal the worker thread to terminate.
' For this custom command, the main service
' continues to run without a worker thread.
OnStop()
Case SimpleServiceCustomCommands.RestartWorker
' Restart the worker thread if necessary.
OnStart(Nothing)
Case SimpleServiceCustomCommands.CheckWorker:
' Log the current worker thread state.
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Worker thread state = " + _
workerThread.ThreadState.ToString(), _
"OnCustomCommand")
Case Else
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Unrecognized custom command ignored!", _
"OnCustomCommand")
End Select
End Sub
' Define a simple method that runs as the worker thread for
' the service.
Private Sub ServiceWorkerMethod()
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Starting the service worker thread.", _
"Worker")
Try
Do
' Wake up every 10 seconds and write
' a message to the trace output.
Thread.Sleep(10000)
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - heartbeat cycle.", _
"Worker")
Loop While True
Catch e As ThreadAbortException
' Another thread has signaled that this worker
' thread must terminate. Typically, this occurs when
' the main service thread receives a service stop
' command.
' Write a trace line indicating that the worker thread
' is exiting. Notice that this simple thread does
' not have any local objects or data to clean up.
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Thread abort signaled.", _
"Worker")
End Try
Trace.WriteLine(DateTime.Now.ToLongTimeString() + _
" - Exiting the service worker thread.", _
"Worker")
End Sub
End Class
End Namespace