Link to home
Start Free TrialLog in
Avatar of kunal_hopra28
kunal_hopra28

asked on

Killing the process associated with a Windows service programatically?

Hey experts,

I have a question that I'm struggling with and would love your help with this. Let me start with the logic of what I'm trying to do.

Logic:

1. There is a windows service running. eg. Name = "Service A"
2. I want to stop the service.
3. I have an associated timeout for the service. eg. 2000
4. if the timeout occurs and the service doesnt stop in that time, I want to kill the process associated with the service.

Question:

How do I kill the process associated with the service, when I only have the name of the service. Basically, if I'm using serviceController, How do I kill the service?

I have some psuedo code here. Please advice.

Thanks,
ServiceController service  = new ServiceController("Service A");
 
try 
{
     service.stop();
     service.waitforstatus(STOPPED, 2000);
}
catch 
{
     // need to kill the process associated with the service. But only have the service name  
     // with me i.e. 'Service A'
}

Open in new window

Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I come with a complex solution for a complex problem in a short amount of time and you don't follow up with why it is not good enough for you, yet you grade low. Please read the 10 pts must principle of EE and give us a chance to help you and to earn that grade. Remember that the expert you grade today you may need to rely on tomorrow.
Avatar of kunal_hopra28
kunal_hopra28

ASKER

Thanks Admin for doing this. I was going to do this this afternoon. Sorry Abel for giving the lower grade. It was a mistake on my part since I had not read the rules that you sent me.

I appreciate your effort and dedication in helping me with this. I look forward to your expertise in the future as well.