等到程序不再运行,或直到超时已过,先发生者为准 [英] Wait till process is no longer running OR until timeout has passed, whichever happens first
问题描述
我要杀死的机器上运行使用的taskkill一个过程,如果他们在X秒后仍在运行(在Windows服务已停止,但仍需要时间进程dissapear) 什么是实现上述在C#中最正确的方式(.NET 2.0或可能3.0)?
I want to kill a process running on the machine using taskkill if they're still running after X seconds (a windows service was stopped but it takes time for processes to dissapear) What's the most correct way to accomplish the above in C# (.net 2.0 or possibly 3.0)?
我有实用的方法来验证进程是否仍在运行,给出的进程名(使用 Process.GetProcesses()
)。
(由于程序不被我的code催生,我不能用WaitTillExit知道什么时候它不再运行)
I've utility method for verifying whether a process is still running, given the process name (using Process.GetProcesses()
).
(As the process is not spawned by my code, I can't use WaitTillExit to know when it's no longer running)
PS:该进程运行在远程计算机上
PS: the process runs on a remote machine
推荐答案
其实,你可以使用Process.WaitForExit()。
In fact, you can use Process.WaitForExit().
简单地得到通过
Process p = Process.GetProcessById(iYourID);
然后调用
if(!p.WaitForExit(iYourInterval))
{
p.Kill();
}
您也可以通过他们的名字流程通过
You can also get processes via their name by
Process.GetProcessesByName(strYourProcessName);
这篇关于等到程序不再运行,或直到超时已过,先发生者为准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!