1

我的代码在下面列出。当我单击按钮时,它应该查看服务,然后确定它需要做什么。但是,当它让部件检查它是否已停止时,它会出现错误,因为它试图启动它,即使它已经启动了。我不知道为什么它试图启动服务,即使它应该返回一个 false 停止。

任何帮助,将不胜感激。

    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
4

1 回答 1

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

享受!

于 2012-11-16T13:48:01.597 回答