Creating a service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone have some sample code (in VB.Net) to create a service that uses a
timer to run a function every hour?

Here's my code:
Protected Overrides Sub OnStart(ByVal args() As String)
Dim tmrState As New TimerState

' make a starting entry in the event log
EventLog.WriteEntry("FaxStatus service starting: " &
System.DateTime.Now)
' create a delegate for handling the ticking of the timer
Dim timerDelegate As New TimerCallback(AddressOf CheckStatus)
' create a timer thats waits for 1 min then ticks every hour
Dim tmrTick As New Timer(timerDelegate, tmrState, 1800000, 3600000)
' keep a handle to the timer so it can be disposed
tmrState.tmr = tmrTick
' the main thread does nothing until the timr is disposed
While Not (tmrState.tmr Is Nothing)
Thread.Sleep(0)
End While
End Sub

thx...sonny
 
Sonny,

The OnStart method must return in a timely manner. Otherwise, the
service control manager will think something is wrong. What you need
to do is declare tmrTick at the class level, instantiate and start it
in the OnStart method, and let the OnStart method return immediately.

Brian
 
Back
Top