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=TrueImports SystemImports System.ComponentModelImports System.IOImports System.ServiceProcessImports System.ThreadingImports System.DiagnosticsNamespace ServiceSample' Define custom commands for the SimpleService.Public Enum SimpleServiceCustomCommandsStopWorker=128RestartWorker=129CheckWorker = 130End Enum' Define a simple service implementation.Public Class SimpleService Inherits System.ServiceProcess.ServiceBasePrivate Const logFile = "C:\service_log.txt"Private Shared Dim serviceTraceListener As TextWriterTraceListener = NothingPrivate Dim workerThread As Thread = NothingPublic Sub New()CanPauseAndContinue = trueServiceName = "SimpleService"End SubPublic 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 = TrueTrace.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 = NothingmyFile.Close()End SubPrivate Sub InitializeComponent()' Initialize the operating properties for the service.Me.CanPauseAndContinue = TrueMe.CanShutdown = TrueMe.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 IfIf Not workerThread Is NothingTrace.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.IsAliveTrace.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 IfIf Not workerThread Is NothingTrace.WriteLine(DateTime.Now.ToLongTimeString() + _" - Worker thread state = " + _workerThread.ThreadState.ToString(), _"OnStop")End IfEnd 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)) = 0Trace.WriteLine(DateTime.Now.ToLongTimeString() + _" - Suspending the service worker thread.", _"OnPause")workerThread.Suspend()End If If Not workerThread Is NothingTrace.WriteLine(DateTime.Now.ToLongTimeString() + _" - Worker thread state = " + _workerThread.ThreadState.ToString(), _"OnPause")End IfEnd 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)) <> 0Trace.WriteLine(DateTime.Now.ToLongTimeString() + _" - Resuming the service worker thread.", _"OnContinue")workerThread.Resume()End If If Not workerThread Is NothingTrace.WriteLine(DateTime.Now.ToLongTimeString() + _" - Worker thread state = " + _workerThread.ThreadState.ToString(), _"OnContinue")End IfEnd 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 commandCase 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 ElseTrace.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 TrueCatch 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 TryTrace.WriteLine(DateTime.Now.ToLongTimeString() + _" - Exiting the service worker thread.", _"Worker")End SubEnd ClassEnd Namespace
Remember Me