更新:2007 年 11 月
可以使用 Responding 属性确定进程的用户界面是否正在响应。尝试读取 Responding 属性时,将向目标进程的用户界面发送一个请求。如果立即有响应,则返回属性值为 true;如果界面没有响应,则返回 false 属性值。如果需要强制冻结的应用程序关闭,该属性很有用。
确定进程是否正在响应
如果进程不是通过组件启动的,请将 Process 组件与目标进程关联。有关更多信息,请参见如何:绑定到现有进程。
读取 Responding 属性。
根据属性值确定要执行的一系列操作。
下面的示例显示如何确定“记事本”是否正在响应。如果 Responding 属性为 true,将调用 CloseMainWindow 方法来关闭应用程序。如果 Responding 属性为 false,将调用 M:System.Diagnostics.Process.Kill 方法来强制关闭进程。
Dim myProcesses() As Process myProcesses = Process.GetProcessesByName("Notepad.exe") ' Tests the Responding property for a True return value. If myProcesses(0).Responding Then myProcesses(0).CloseMainWindow() Else ' Forces the process to close if the Responding value is False. myProcesses(0).Kill() End If
Process[] notepads; notepads = Process.GetProcessesByName("Notepad.exe"); // Test to see if the process is responding. if (notepads[0].Responding) { notepads[0].CloseMainWindow(); } else { notepads[0].Kill(); }