程序无响应判断
- dotnet的Process自带Responding属性
- IsHungAppWindow系统api(window xp以后api失效)
- SendMessageTimeout发送窗口消息
- 通过共享内存,获取程序心跳数据的方式(需要程序写共享内存)
- FindWIndow遍历窗口,查询特定窗口的错误信息,发送WM_CLOSE关闭(准确率高,权限要求低)
Process.GetProcessesByName(Path.GetFileNameWithoutExtension(objSelectItem)).
Concat(Process.GetProcessesByName("WerFault")).
Concat(Process.GetProcessesByName("csrss")).
ToList().ForEach(o =>
{
if (o.ProcessName == Path.GetFileName(objSelectItem) ||
o.MainWindowTitle == "Microsoft Visual C++ Runtime Library" ||
o.MainWindowTitle == "Microsoft Visual Studio" ||
o.MainWindowTitle.Contains("- 系统错误") ||
o.MainWindowTitle.Contains("- 应用程序错误")
)
{
try
{
Program.SendMessageTimeout(o.MainWindowHandle, Program.WM_CLOSE, IntPtr.Zero, IntPtr.Zero, 0, 1000, IntPtr.Zero);
}
catch { }
}
else
{
if (o.MainWindowHandle != IntPtr.Zero && o.MainWindowTitle.Contains(Path.GetFileName(objSelectItem)))
Program.SendMessageTimeout(o.MainWindowHandle, Program.WM_CLOSE, IntPtr.Zero, IntPtr.Zero, 0, 1000, IntPtr.Zero);
Thread.Sleep(3000);
}
});
程序异常重启
程序执行短暂的代码后自动重启,可以达到用户无法关闭的效果,而且如果是管理员运行,非管理员无法关闭
Application.Restart();
Process.GetCurrentProcess().Kill();