Creating a service

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
 
B

Brian Gideon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top