I'm trying to write an application that will periodically check a server
for certain services and, if they are not running, attempt to start them.
I'm using System.ServiceProcess and have no trouble detecting which
services are up and which are stopped, regardless of what platform my
application is running on.
However, when I try to start services, success is very intermittent. As far
as I can tell, running on XP or 2000 makes it impossible to start a service
on NT4 servers, but running on NT4 it sometimes works.
Is there a known OS issue that may be preventing this? My restart script in
VB (I have also tried using C# with the same results) is:
Public Function StartStopped(sServer as String, sService as String) as
Boolean
Dim oServices as New System.ServiceProcess.ServiceController()
Dim oService as System.ServiceProcess.ServiceController()
oServices.MachineName = sServer
For Each oService in oServices.GetServices()
if oService.ServiceName = sService Then
oService.Start()
oService.oServiceToStart. _
WaitForStatus(ServiceControllerStatus.Running, _
System.TimeSpan.FromSeconds(15.0))
if oService.ServiceControllerStatus = _
ServiceControllerStatus.Running Then
StartStopped = True
Exit Function
else
StartStopped = False
Exit Function
end if
end if
Next
End Function