I'm using VB.NET 2005, and I'm looking to put together a simple service. I already wrote this as a regular application, but I figure it will be a lot more useful as a service (where it always runs, and doesn't need to have a user logged in).
The idea behind the service is that it simply checks a few folders and the status of another service at a regular interval. To do this, I have a timer that fires every minute. The system itself works fine, the part that doesn't work now that I have it in the form of the service is that the service stops and immediately stops. I get a message that says "The XMonitor service on Local COmputer started and then stopped. Some services top automatically if they have no work to do, for example, the Performance Logs and Alerts service."
My OnStart sub looks like this:
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
If Not EVENT_LOGGER.SourceExists("XMonitor") Then
EVENT_LOGGER.CreateEventSource("XMonitor", "XMonitor Log")
End If
LoadConfiguration()
EVENT_LOGGER.Source = "XMonitor"
EVENT_LOGGER.WriteEntry("XMonitor service started.", EventLogEntryType.Information, 1000)
MainStart()
End Sub
All LoadConfiguration() does is load the interval to perform the checks with (in case the user wants to override the default). MainStart is what actually starts the timer. These functions are all in a separate module I created. The MainStart sub and the timer I use:
Public WithEvents CheckTimer As New Timers.Timer
Public Sub MainStart()
CheckTimer.Enabled = True
CheckTimer.Interval = 60000
End Sub
The Tick event for the timer (for simplified, testing purposes):
Private Sub CheckTimer_Tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles CheckTimer.Elapsed
MsgBox("Tick!")
End Sub
From what I've read, I believe that I'm doing everything properly -- but I must be missing something. This is the first Windows service I've tried to write, so forgive my newbishness.
Any ideas