473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Service unable to start

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(ServicesToRu n)
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
************************************************** **********************************
May 20 '06 #1
3 3783
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

"tshad" <ts**********@ftsolutions.com> wrote in message
news:uJ*************@TK2MSFTNGP04.phx.gbl...
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(ServicesToRu n)
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
************************************************** **********************************

May 20 '06 #2
>> Could the "System.Threading.Thread.Sleep(60000)" line be causing a
problem????


Sure seems like it to me.

May 20 '06 #3
tshad wrote:
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.
May 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kevin Davidson | last post by:
I am trying to get a clean installation of the .NET framework 1.1 on Windows 2000 Professional. I had previously installed a beta of the 2.0 Framework and uninstalled it. I also uninstalled...
2
by: Billy Cormic | last post by:
Hello, I have successfully created a windows service. I would now like the windows service to call another program and start it when the service starts. I have been unable to do this. For the...
4
by: Jeff | last post by:
Hi - I'm building a VB.NET Windows application with an MSDE database. Anticipating the possibility that the MSDE service may have been stopped outside of my app, I'm trying to code a way to...
3
by: Amjad | last post by:
Hi, I just wrote a test Windows Service that creates a text file on startup (please see my code below). The file is never created. Protected Overrides Sub OnStart(ByVal args() As String) Dim...
10
by: amirmira | last post by:
I have a Windows Service developed in VB.NET that attempts to connect to a database as soon as the service starts. I have no problem when I start the service manually - but when I restart the...
2
by: Trevor | last post by:
Argh! This problem is driving me nuts! Can you help? In November of 2003, I installed a web service on Windows Server 2003 built in VB.NET for v1.1.4322 of the framework. It contains a timer...
3
by: Maqsood Ahmed | last post by:
I have made a windows service which is configured to start automatically at the system startup under the USER account type. The service is working perfectly fine when I try to start it manually,...
9
by: Yasir Zaheer | last post by:
I have made a windows service which is configured to start automatically at the system startup under the USER account type. The service is working perfectly fine when I try to start it manually,...
4
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.