Service unable to start

T

tshad

I created a service in VS 2003 and the code works fine as an application,
but not as a service.

It installs fine, but when I try to start it, it tells me it was unable to
start.

Am I missing something here?

Here is the program.

*************************************************
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports System.ServiceProcess

Public Class Service1
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase () {New
Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub

#End Region

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.
Call RunIt()
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
End Sub

Public Sub RunIt()
While (2 > 1)
tmrProcCheck()
System.Threading.Thread.Sleep(60000) '1 minute, time is in
milliseconds
End While
End Sub
Private Sub tmrProcCheck()
Dim errors As Boolean = False
Dim errorMessage As String
Dim theProcess() As Process

theProcess = Process.GetProcessesByName("cmd")
If theProcess.Length = 0 Then
Process.Start("notepad.exe")
End If

End Sub

End Class
**************************************************************************************

The code I wrote was (without the code MS added):

******************************************************************************
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports System.ServiceProcess

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.

Call RunIt()

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
End Sub

Public Sub RunIt()
While (2 > 1)
tmrProcCheck()
System.Threading.Thread.Sleep(60000) '10 minutes, time is in
milliseconds
End While
End Sub
Private Sub tmrProcCheck()
Dim errors As Boolean = False
Dim errorMessage As String
Dim theProcess() As Process

theProcess = Process.GetProcessesByName("cmd")
If theProcess.Length = 0 Then
Process.Start("notepad.exe")
End If

End Sub

End Class
************************************************************************************
 
T

tshad

I just checked using the debugger and it is running and going through loops
fine.

But in the services window it says starting, and in the event log it says:

Service cannot be started. The service process could not connect to the
service controller

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.

When I try to uninstall it, it says it does, but it is still there in the
Services window say it is "Starting" and it stays there until I reboot the
machine (even if I say refresh).

Could the "System.Threading.Thread.Sleep(60000)" line be causing a
problem????

Thanks,

Tom
 
C

Cerebrus

Could the "System.Threading.Thread.Sleep(60000)" line be causing a
Sure seems like it to me.
 
P

Phill W.

tshad said:
I created a service in VS 2003 and the code works fine as an application,
but not as a service.

It installs fine, but when I try to start it, it tells me it was unable to
start.

When a service is started (by Windows), Windows invokes the OnStart
method within the Service. Only when this method /returns/ will Windows
be "happy" that the service is running.
Your code does not return from OnStart because OnStart calls RunIt which
is your "main" processing loop.

You need to break out of this Call-and-Return chain so that OnStart can
return promptly to Windows, and the service will be seen as running
"properly".

Personally, I use a [System.Timers.]Timer for this, starting it in
OnStart and, when it fires, immediately disabling it again then running
the "main" routine, something like:

Private Sub OnStart( ...
tmrGo.Interval = 50
tmrGo.Start()
' Go straight back to Windows
End Sub

Private Sub tmrGo_Tick( ... ?
tmrGo.Stop()
RunIt()
End Sub

Also, I'd suggest reducing this Sleep interval ...

System.Threading.Thread.Sleep(60000)

.... to something smaller, even if you have to execute lots of little
Sleep's in a loop. When you /stop/ a Service, Windows will "ask" the
service to stop - if it's stuck in a Sleep() call, it can't respond, and
you'll get errors from the Service Manager.

A word of caution about /stopping/ your service - If your Service is
doing large "chunks" of work, make sure OnStop allows the current
"chunk" to finish before returning to Windows, something like

Private m_bStopRequested As Boolean = False
Private m_bStopped As Boolean = False

Private Sub RunIt( ...
m_bStopped = False
Do While Not m_bStopRequested
ProcessAJobofWork()
SleepForABit()
Loop
m_bStopped = True
End Sub

Private Sub OnStop( ...
m_bStopRequested = True
Do While Not m_bStopped
Sleep( 1000 )
Loop
End Sub

If you don't, then as soon as you return from OnStop, your Service'
process gets killed stone dead (at least mine did). :)

Finally, I assume this ...

Process.Start("notepad.exe")

... is just "testing" code. Services are /not/ intended to be visible or
to interact with the desktop - they're supposed to be used for things
that don't need anyone logged on to the machine.

HTH,
Phill W.
 

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