Starting a Windows Service Programattically
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?
Re: Starting a Windows Service Programattically
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:
Code:
Using scnService As New ServiceController
With scnService
.MachineName = "."
.ServiceName = "SERVICENAME"
.Start()
End With
End Using
Good luck
Re: Starting a Windows Service Programattically
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
Re: Starting a Windows Service Programattically
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..
Re: Starting a Windows Service Programattically
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
Re: Starting a Windows Service Programattically
I just tried this:
Code:
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?
Re: Starting a Windows Service Programattically
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?
Code:
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?
Re: Starting a Windows Service Programattically
Quote:
Originally Posted by
johntheface
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!
Re: Starting a Windows Service Programattically
It comes with the territory, don't worry about it.. at least you found the problem :)