2012-11-15 2 views
1

내 코드는 아래와 같습니다. 버튼을 클릭하면 서비스를 살펴본 다음 필요한 작업을 결정해야합니다. 그러나 멈추었는지 검사 할 부분이 생기면 이미 시작되었지만 오류가 발생하기 때문에 오류가 발생합니다. 나는 그 서비스를 시작하려고 애쓰는 이유를 알지 못한다.VB.Net System.ServiceProcess 서비스를 확인하려고 할 때 오류가 발생했습니다.

도움을 주시면 감사하겠습니다.

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

범프, 누군가 이걸 알고 있어야합니다 .... – user1827407

답변

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 

즐기십시오!

관련 문제