I need to check if a server is running and then start the server if it is not running. I am having trouble with the ServiceState never updating. Ie... after I start the service I cannot check if its actually running using ServiceState.
In the following example I start a server that is stopped wait 30 seconds and then display its ServiceState it is allways comming back as stopped even when I know the service is running. If I make a new service object and set it to the server the server state property will correctly return running.
I am baffled by this any ideas on this?
Dim myComp As New ManagedComputer(System.Environment.MachineName)
Dim myservice As Service
myservice = myComp.Services("MSSQL$KBMSS")
myservice.Start()
Dim StartTime As TimeSpan = Date.Now.TimeOfDay
Do While Date.Now.TimeOfDay.Subtract(StartTime).TotalSeconds < 30
If myservice.ServiceState = ServiceState.Running Then Exit Do
Loop
MessageBox.Show(myservice.ServiceState.ToString)
Add myservice.Refresh() before the service state check and you're golden.
SMO is a cached object model, so it means you need to refresh the object state if needed.
No comments:
Post a Comment