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

さて、これが私が問題を修正するためにしたことです。

2つの異なるifステートメントを使用する代わりに、1つに結合しました。

    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 に答える