C# 杀死所有对运行 Windows 不重要的进程
我有一个申请。如果该应用程序在 15 分钟内未使用,则需要关闭所有其他应用程序(强制关闭)并且计时器再次启动。我不想这样做让 Windows 7 崩溃。到目前为止,我有以下内容:
Process me = Process.GetCurrentProcess();
foreach (Process p in Process.GetProcesses())
{
if (p.Id != me.Id
&& p.ProcessName != "winlogon.exe"
&& p.ProcessName != "explorer.exe"
&& p.ProcessName != "System Idle Process"
&& p.ProcessName != "taskmgr.exe"
&& p.ProcessName != "spoolsv.exe"
&& p.ProcessName != "csrss.exe"
&& p.ProcessName != "smss.exe"
&& p.ProcessName != "svchost.exe "
&& p.ProcessName != "services.exe"
)
{
p.Kill();
}
}
遗憾的是 Windows 死机了(蓝屏)。有什么方法可以关闭所有正在使用的进程,然后希望 Windows 能够生存下来。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,在公共PC上设置自动登录。
然后,您的程序只需重新启动即可。
如果您设置稳定状态或使用 Deep Freeze、系统安全,或时间冻结一个>。这些产品甚至可以选择在一段时间不活动后将计算机重新启动到干净状态......
First, set up auto-logon on the public PC.
Then, your program just has to reboot.
Bonus points if you set up steady state or use a product like Deep Freeze, System Safe, or Time Freeze. Those products even have an option for rebooting the computer to a clean state after a period of inactivity...
确保安全的最简单方法是保留应用程序启动时存在的进程的列表,并将其排除在终止之外。但是,根据应用程序启动的时间(例如,如果它不是系统启动的一部分),这可能会让某些应用程序漏掉。从好的方面来说,它将允许设备的用户模式挂钩(例如鼠标和键盘处理程序)继续运行,这意味着在您杀死所有内容后系统仍然可用。
您可以尝试广播
WM_CLOSE
消息,但如果 Windows 阻止它,我不会感到惊讶。它只会对具有可见窗口的进程产生影响,但这可能就足够了。第三种选择是使用
ExitWindowsEx
这将让操作系统处理关闭所有内容。当然,这也会关闭您自己的应用程序并强制用户重新登录。如果您设置了自动登录,那么它可能会直接重新登录。The easiest way to be safe here is to keep a list of those processes that existed when your application started and exclude those from the kill. However, depending on when your application starts (if it isn't part of the system startup, for example) this may let some applications slip through the cracks. On the up side, it will allow user-mode hooks for devices (such as mouse and keyboard handlers) to continue running, which means the system is still usable after you've killed everything.
You could try broadcasting a
WM_CLOSE
message, though I wouldn't be surprised if Windows blocks it. It would only have an effect on processes with visible windows, but that may be sufficient.A third option is to force a logoff with
ExitWindowsEx
that will let the OS handle closing everything. Of course, this will also close your own application and force the user to log on again. If you have an auto-login set up then it may log straight back in.这似乎有效。
This seems to be working.
您显然正在杀死窗口运行所需的重要进程。如果你杀死它们,Windows 将无法恢复(正如你已经意识到的那样)并且需要重新启动。
You apperently are killing vital processes needed for windows to function. If you kill those, windows can't recover (as you realized already) and needs to be rebooted.