我的代码如下所示。当我点击按钮时,它应该查看服务然后确定它需要做什么。但是,当它获取部件以检查它是否已停止时,它会收到错误,因为它尝试启动它,即使它已经启动了。我不知道为什么它试图启动服务,即使它应该返回假的停止。
任何帮助都将不胜感激。
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
答案 0 :(得分:0)
嗯,这就是我为解决这个问题所采取的措施。
我没有使用两个不同的if语句,而是将它组合成一个。
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()
ElseIf sc.Status = System.ServiceProcess.ServiceControllerStatus.Stopped Then
sc.Start()
End If
享受!