My code is listed below. When I click the button, it’s supposed to look at the service and then determine what it needs to do. However, when it gets the part to check if it is stopped, it’s getting an error because its trying to start it, even though it is already started. I don’t know why its trying to start the service even though it should be returning a false for stopped.
Any Help would be appreciated.
Dim sc As New System.ServiceProcess.ServiceController("LPT:One Job Queue Engine")
'This sets the Machine Name/IP Address
'Removed machine name.
sc.MachineName = "**********"
'This tells the service to stop
If sc.Status = ServiceProcess.ServiceControllerStatus.Running Then
sc.Stop()
'This tells the program to wait until the service is stopped
sc.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped)
'This starts the service once it has stopped
sc.Start()
End If
'Here is where the problem is!
**If sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Stopped) Then
sc.Start()
End If**
If sc.Status = ServiceProcess.ServiceControllerStatus.StopPending Then
sc.ExecuteCommand("taskkill /F /IM lptjqe.exe")
sc.Start()
End If
Well, here is what I did to fix the issue.
Instead of having two different if statements, I combined it into one.
Enjoy!