We are using a badly written windows service, which will hang when we are trying to Stop it from code. So we need to find which process is related to that service and kill it. Any suggestions?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Microsoft/SysInternals has a command-line tool called PsKill that allows you to kill a process by name. This tool also allows you to kill processes on other servers. Windows SysInternals
WMI has this information: the Win32_Service class.
A WQL query like
using System.Management should do the trick.
From a quick look see:
taskllist.exe /svc
and other tools from the command line.You can use
To find the process name and id, and also if the same process hosts other services.
You can use
System.Management.MangementObjectSearcher
to get the process ID of a service andSystem.Diagnostics.Process
to get the correspondingProcess
instance and kill it.The
KillService()
method in the following program shows how to do this:I guess it's a two step process - if it's always the same service, you can easily find the process name using methods suggested in other answers.
I then have the following code in a class on a .NET 1.1 web server:
The Kill method can throw a few exceptions that you should consider catching - especially the Win32Exception, that is thrown if the process cannot be killed.
Note that the WaitForExit method and HasExited property also exist in the 1.1 world, but aren't mentioned on the documentation page for Kill in 1.1.
To answer exactly to my question - how to find Process related to some service:
}