sc queryex themes, taskkill /pid 1148 /f

783 Views Asked by At

I want to know how to stop a hanging service with c# like i would do in cmd using sc queryex servicename to get the pid and taskkill /pid pidnumber /f to kill the hanging service...

I have read Stopping windows service with taskkill but this did not give me a solution.

I would like something like this:

Process.GetProcessesByName("aServiceName").Kill();

Which isn't working...

1

There are 1 best solutions below

0
On

Use PsKill. It is an exec utility produced by sysinternals. You can use it in command line mode as well as in programatic mode. Id you want to do that programmatically, I would recommand to create a utility class that has a method that takes a service id, creates a process, using the Process class in C#, attach the process to pskill.exe, passing the parameters to pskill and lauch the process with the start method. Here is a sample:

public static void KillProcess(int _servicePid)
{
    System.Diagnostics.Process.Start("C:\pstools\pskill.exe", _servicePid);
}

PsKill download link: http://technet.microsoft.com/en-us/sysinternals/bb896683.aspx