2

I create a windows service controller. I use this code for stop a service.

private void button1_Click(object sender, EventArgs e)
{
        ServiceController myService = new ServiceController();
        myService.ServiceName = "BITS";
        if ((myService.Status.Equals(ServiceControllerStatus.Running)) || (myService.Status.Equals(ServiceControllerStatus.StartPending)))
        {
            myService.Stop();
            label1.Text = "Service is stop";
        }
}

But when i click stop button. It show correct service status. But it is not working for stop this service. When i click there show a error message that

" An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceProcess.dll , Additional information: Cannot open BITS service on computer '.'. "

Anybody help me to solve this problem.

Akib1314
  • 41
  • 3
  • Is it possible that the service is handling an event which causes the error, i.e. the error has nothing to do with the stopping of the service as such? What happens if you change the service to just do nothing; does the problem remain? – Codor Apr 19 '16 at 10:00
  • Maybe your process doesn't have enough permissions to stop the service – Maxim Kosov Apr 19 '16 at 10:00
  • 2
    Its a UAC permission problem. You need to run the program as administrator, or turn off uac or add this code to make the system run it as administrator. http://stackoverflow.com/questions/3892088/servicecontroller-permissions-in-windows-7 – CathalMF Apr 19 '16 at 10:04
  • Your code snippet says "BITS" but your error says "WSearch". Which is it? Or are you seeing a WSearch errors when stopping BITS, or is this just a copy-paste error to SO? – Avner Shahar-Kashtan Apr 19 '16 at 10:05
  • You can't stop a service if you don't havew the proper permissions. If you have administrative rights, you should start your application with "Run as Administrator". If not, you should give the appropriate permissions to your account. Some system services though *can't* be stopped even by an administrator – Panagiotis Kanavos Apr 19 '16 at 10:11

0 Answers0