473,785 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Diagnost ics
Imports System.Componen tModel
Imports System.Data.Sql Client
Imports System.Web.Mail
Imports System.ServiceP rocess

Public Class Service1
Inherits System.ServiceP rocess.ServiceB ase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase

' 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.ServiceP rocess.ServiceB ase () {New
Service1, New MySecondUserSer vice}
'
ServicesToRun = New System.ServiceP rocess.ServiceB ase () {New
Service1}

System.ServiceP rocess.ServiceB ase.Run(Service sToRun)
End Sub

'Required by the Component Designer
Private components As System.Componen tModel.IContain er

' 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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
components = New System.Componen tModel.Containe r()
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.Threadin g.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.GetProc essesByName("cm d")
If theProcess.Leng th = 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.Diagnost ics
Imports System.Componen tModel
Imports System.Data.Sql Client
Imports System.Web.Mail
Imports System.ServiceP rocess

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.Threadin g.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.GetProc essesByName("cm d")
If theProcess.Leng th = 0 Then
Process.Start(" notepad.exe")
End If

End Sub

End Class
*************** *************** *************** *************** *************** *********
May 20 '06 #1
3 3807
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.Threadi ng.Thread.Sleep (60000)" line be causing a
problem????

Thanks,

Tom

"tshad" <ts**********@f tsolutions.com> wrote in message
news:uJ******** *****@TK2MSFTNG P04.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.Diagnost ics
Imports System.Componen tModel
Imports System.Data.Sql Client
Imports System.Web.Mail
Imports System.ServiceP rocess

Public Class Service1
Inherits System.ServiceP rocess.ServiceB ase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase

' 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.ServiceP rocess.ServiceB ase () {New
Service1, New MySecondUserSer vice}
'
ServicesToRun = New System.ServiceP rocess.ServiceB ase () {New
Service1}

System.ServiceP rocess.ServiceB ase.Run(Service sToRun)
End Sub

'Required by the Component Designer
Private components As System.Componen tModel.IContain er

' 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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
components = New System.Componen tModel.Containe r()
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.Threadin g.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.GetProc essesByName("cm d")
If theProcess.Leng th = 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.Diagnost ics
Imports System.Componen tModel
Imports System.Data.Sql Client
Imports System.Web.Mail
Imports System.ServiceP rocess

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.Threadin g.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.GetProc essesByName("cm d")
If theProcess.Leng th = 0 Then
Process.Start(" notepad.exe")
End If

End Sub

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

May 20 '06 #2
>> Could the "System.Threadi ng.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.Threadin g.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_bStopRequeste d As Boolean = False
Private m_bStopped As Boolean = False

Private Sub RunIt( ...
m_bStopped = False
Do While Not m_bStopRequeste d
ProcessAJobofWo rk()
SleepForABit()
Loop
m_bStopped = True
End Sub

Private Sub OnStop( ...
m_bStopRequeste d = 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
1458
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 framework 1.1 and 1.0 and reinstalled 1.1 and service pack 1. When I attempt to start the ASP.NET State Service, I get event log: Event Type: Error Event Source: Service Control Manager Event Category: None
2
1332
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 windows service OnStart sub I have the following code: Protected Overrides Sub OnStart(ByVal args() As String) EventLog1.WriteEntry("In OnStart") Process.Start("Explorer.exe")
4
2249
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 start it when my app is launched. I can't seem to get the code to wait for MSDE to start before continuing (the next step is opening a connection). My development machine is WinXP Pro. I'm using the ServiceController class to start and check the...
3
14952
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 swLog As StreamWriter = File.CreateText("C:\myLog.txt") swLog.WriteLine("My Windows Service has just started.") swLog.Close() : swLog.Flush() End Sub
10
1912
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 computer that hosts the service, I get a Null Reference Exception when I try to connect to the database (I have set the service to start automatically). Any ideas why this is happening? Thanks in advance. Ajay Mirmira
2
2460
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 (System.Timers.Timer) which has an interval of 24 hours. Actually, it reads a time like 2AM out of the config file, and calculates the time between the start of the service to 2AM, and sets the timer. When the timer expires, it re-reads the...
3
2697
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, but it is unable to start automatically at system restart and following errors are displayed in the Event Logs. 1 - Timeout (30000 milliseconds) waiting for the (Service Name) service to connect. 2 - The (Service Name) service failed to start...
9
3478
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, but it is unable to start automatically at system restart and following errors are displayed in the Event Logs. 1 - Timeout (30000 milliseconds) waiting for the (Service Name) service to connect. 2 - The (Service Name) service failed to start...
4
8847
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 windows service that converts MS Word Files into .PDF files and after that we want to zip the .PDF files. Our code: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.