Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试检查服务时出现VB.Net System.ServiceProcess错误_Vb.net - Fatal编程技术网

尝试检查服务时出现VB.Net System.ServiceProcess错误

尝试检查服务时出现VB.Net System.ServiceProcess错误,vb.net,Vb.net,下面列出了我的代码。当我点击按钮时,它应该查看服务,然后确定它需要做什么。然而,当它让部件检查它是否停止时,它得到了一个错误,因为它试图启动它,即使它已经启动了。我不知道为什么它试图启动服务,即使它应该返回一个false for stopped 任何帮助都将不胜感激 Dim sc As New System.ServiceProcess.ServiceController("LPT:One Job Queue Engine") 'This sets the Machine Na

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

任何帮助都将不胜感激

    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

下面是我为解决这个问题所做的

我没有使用两个不同的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

享受吧

邦普,来吧,有人应该知道这个。。。。