找出Windows服务正在运行的进程名称.NET 1.1
我们正在使用一个写得很糟糕的 Windows 服务,当我们试图从代码中阻止它时,它会挂起。 因此,我们需要找到与该服务相关的进程并将其杀死。 有什么建议么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
System.Management.MangementObjectSearcher
获取服务的进程 ID 和System.Diagnostics.Process
获取相应的Process
实例并杀死它。以下程序中的
KillService()
方法显示了如何执行此操作: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:WMI 有以下信息:Win32_Service 类。
像使用 System.Management 这样的 WQL 查询
应该可以解决问题。
快速查看一下:
taskllist.exe /svc
以及命令行中的其他工具。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.您可以使用
查找进程名称和 ID,以及同一进程是否托管其他服务。
You can use
To find the process name and id, and also if the same process hosts other services.
要准确回答我的问题 - 如何找到与某些服务相关的进程:
}
To answer exactly to my question - how to find Process related to some service:
}
Microsoft/SysInternals 有一个名为 PsKill 的命令行工具,它允许您按名称终止进程。 该工具还允许您终止其他服务器上的进程。 Windows SysInternals
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
我想这是一个两步过程 - 如果它始终是相同的服务,您可以使用其他答案中建议的方法轻松找到进程名称。
然后,我在 .NET 1.1 Web 服务器上的类中包含以下代码:
Kill 方法 可能会抛出一些您应该考虑捕获的异常 - 特别是 Win32Exception,如果进程无法被终止,则会抛出该异常。
请注意 WaitForExit 方法 和 HasExited 属性也存在于 1.1 世界中,但是1.1 中的 Kill 文档页面中没有提及。
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.