ServiceController.start() and ServiceController.stop() are throwing exceptions?

前端 未结 5 1545
野趣味
野趣味 2021-01-21 07:43

The following code is throwing Exception. I don\'t get what mistake I am making in the code. Can somebody help me figure out please. I think it is of some security rights issue.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 08:25

    If you are already having the Service user as LocalSystem(the high privilage user) the problem is not security. Also I had that problem before and its the status vrs starting it again or stopping it when already commande to stop().

    You see the service status its not changed on demand, so even if you coded

    //this will start the process but the 
    //sc status will take some time to change
    //when that happens and you try to start 
    //the already started service it will give you 
    //your error
    servicec.start();
    

    Soo you need to do this: msdn ServiceController.waitforstatus

     Dim sc As New ServiceController
         sc.ServiceName = "DataLoad"   
    If sc.Status = ServiceControllerStatus.Stopped     Then     
        sc.Start()    
     // this makes it wait for the status to change
      // and no it wont slow down anything at all. 
    sc.waitforstatus(ServiceControllerStatus.started)
    Else  
        sc.Stop()
    sc.waitforstatus(ServiceControllerStatus.stopped)
    End If
    

    This will solve your problem like it did mine.

提交回复
热议问题