Nicolai
Nicolai

Reputation: 2915

Stopping a service

I'm trying to stop a running service, with a console program I've made. This is my code:

ServiceController sc = new ServiceController(ServiceName, ".");
sc.Stop();

I then wait for the service status, to turn into Stopped, but this never happens. It seems to have StopPending and never goto Stopped. When I check Services.msc, it is listed as stopped, and in the taskmanager, the process is gone.

Will it stay on Pending forever? Am I doing something wrong? I have the code for the service aswell, if I need to modify something there.

Upvotes: 1

Views: 1832

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Am I doing something wrong?

You may try waiting after stopping the service:

sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(25));

The WaitForStatus method will wait for the service to reach the specified status or for the specified time-out to expire before returning.

Upvotes: 5

Related Questions