简体   繁体   中英

sc queryex themes, taskkill /pid 1148 /f

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM