VB.NET Windows Service

S

Steven Thomas

I am writing an window service application in vb.net.

I have a sub that when called will start looping and doing work as
long as the service is running.

The problem I have is when I call the sub from the onstart sub the
service never thinks it has started. It remains in a "Starting"
state. Therefore I can not stop the service either.

All of the examples I can find just show turning on a timer that
writes to the event log, but no real examples of how to do some work
in the service.

Anyone know how to do this?

Thanks

/*--------------------- My code example ----------------*/
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.
StartService()
End Sub

Public Sub StartService()
Try
Dim IsRunning As Boolean = True
While IsRunning
' LOOP THROUGH THE WORK THE SERVICE IS PREFORMING
End While
Catch
' Handle errors
End Try
end sub
/*-------------------------------------------------------*/
 
C

Cor Ligthert

Hi Steven,

When I see the problem right is for this the simplest to use something as

threading.thread.sleep(1000) ' sleeps one second
application.doevents ' when needed to stop by instance

This is the main thread before you think it is threading.

I hope this helps?

Cor
 
M

mwazir

Steven,

You are calling a subroutine from your OnStart that is running an infinite
loop. Basically what that means is that your OnStart procedure never exits.
Thats causing problems for the Service Control Manager because it expects
you to complete your OnStart within a timeframe (I think it is 60 seconds,
not sure)

You can either use a timer and run your procedure on a seperate thread.

How about something like this for a timer. I havent tested this for any
syntax or compile errors though. But this should give you a basic idea.

Private oTimer As System.Threading.Timer

Protected Overrides Sub OnStart(ByVal args() As String)
Dim oCallback As New TimerCallback(AddressOf OnTick)
Dim lPeriod as Long = 100
oTimer = New System.Threading.Timer(oCallback, Nothing, 0, lPeriod)
End Sub

Public Sub OnTick(ByVal state As Object)
Try
' Do Something here
Catch e as exception
debug.writeline (e.Message)
End Try
End Sub
HTH
 
S

Steven Thomas

Cor
I don't think you understand the question.

I need to start the service and call the sub routine to so the work can
be done. Currently i am calling the sub from the on start. How ever
the service never thinks it completely starts because the sub never
finshes. This is by design. So how can I start the sub from the
onstart and allow the service to think it has started?
 
P

Patrick Steele [MVP]

I am writing an window service application in vb.net.

I have a sub that when called will start looping and doing work as
long as the service is running.

The problem I have is when I call the sub from the onstart sub the
service never thinks it has started. It remains in a "Starting"
state. Therefore I can not stop the service either.

All of the examples I can find just show turning on a timer that
writes to the event log, but no real examples of how to do some work
in the service.

Anyone know how to do this?

You could start up another thread to do the work. OnStart could have
something like (not exact -- just typing off the top of my head):

sub ...
dim bt as thread = new thread(new threadstart(
addressof(StartService))
)
bt.Background = true
bt.Start()
end sub
 
S

Steven Thomas

Patrick Steele said:
You could start up another thread to do the work. OnStart could have
something like (not exact -- just typing off the top of my head):

sub ...
dim bt as thread = new thread(new threadstart(
addressof(StartService))
)
bt.Background = true
bt.Start()
end sub

Thanks Patrick
I used your example and it works great
 

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