Click to See Complete Forum and Search --> : Starting a Windows Service Programattically


johntheface
December 12th, 2008, 04:49 AM
I have a program which sets the .config file of a windows service and then starts the service. The problem I'm having i can only get the service to start if the program terminates immediately after starting the service, however i want to wait for 30 seconds and then stop the service before terminating the program.

the code i use to start the service is:

scnService = New ServiceController

With scnService
.MachineName = "."
.ServiceName = "SERVICENAME"
.Start()
End With

End

After doing some investigating i found that if i put any code after .Start() the Status of the Service is set to 2 (StartWithPending - i don't know what this means) however if i immediately terminate the program the Status of the Service is set to 4 (Running). How do i set the Status to 4 without terminating the program?

HairyMonkeyMan
December 12th, 2008, 09:14 AM
Is the servicecontroller managed or unmanaged code? If managed, it should dispose when the variable with the object reference goes out of scope (probably whats happening when you exit your program). If its unmanaged, try calling scnService.Dispose()

If unmanaged, and your using vs2005, you can do something like this:

Using scnService As New ServiceController
With scnService
.MachineName = "."
.ServiceName = "SERVICENAME"
.Start()
End With
End Using

Good luck

johntheface
December 15th, 2008, 03:42 AM
I tried both examples but go the same problem with each. also i didn't write the service so i don't have much information on it either which i know isn't very helpful

GremlinSA
December 15th, 2008, 03:55 AM
I just got hit by something ....

Arn't you trying to start the service in the same thread as your App, try starting the Service in a new thread.

Gremmy..

johntheface
December 15th, 2008, 04:07 AM
i tried threading but it didn't work and i got the same problem. I don't really know much about threading so it may be that i didn't write the code properly. here's what i did:

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

Dim t As Threading.Thread
t = New Threading.Thread(AddressOf Me.StartService)
t.Start()

End Sub

Private Sub StartService()

With scnService
.MachineName = "."
.ServiceName = "SERVICENAME"
.Start()
.Dispose()
Application.DoEvents()
End With

End Sub

i also tried writing the StartService() sub like this and it still didn't work:

Private Sub StartService()
Using scnService As New ServiceController
With scnService
.MachineName = "."
.ServiceName = "SERVICENAME"
.Start()
Application.DoEvents()
End With
End Using
End Sub

HairyMonkeyMan
December 16th, 2008, 04:11 AM
I just tried this:

Private scnService As ServiceProcess.ServiceController

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
StartService()
MsgBox("Done")
End Sub


Private Sub StartService()
scnService = New ServiceProcess.ServiceController("Apache2.2", ".")
scnService.Start()
End Sub

I didn't get the 'start with pending' status message in my services, just 'started' as expected. Can you post some sample code that reproduces the problem?

johntheface
December 16th, 2008, 09:16 AM
thank you everyone for your help. i now know the problem occurs if i try to write data to the .config file of the windows service using a StreamWriter immediately before starting the service. I'm assuming that my code is making the config file unreadable for the service?


Using swrConfigFile As New StreamWriter("C:\My Service.config")
With swrConfigFile
.WriteLine(strConfigData)
.Close()
End With
End Using

Dim scnService = New ServiceController("SERVICENAME", ".")
scnService.Start()


I know that the data being written to the file is correct and i've tried writing to the file in a Using statement and i've tried calling swrConfigFile.Dispose() once i've finished but i still get the error. any ideas?

johntheface
December 16th, 2008, 09:23 AM
I know that the data being written to the file is correct

Actually it wasn't and that's what was causing the error. sorry for wasting everyone's time!

HairyMonkeyMan
December 16th, 2008, 10:33 AM
It comes with the territory, don't worry about it.. at least you found the problem :)