C# 杀死所有对运行 Windows 不重要的进程

发布于 2024-10-18 14:35:23 字数 800 浏览 1 评论 0 原文

我有一个申请。如果该应用程序在 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 能够生存下来。

I have an application. If the application is not used in 15 mins it needs to closes all other applications (forced closed) and the timer starts again. I do not want to crash windows 7 doing this. So far I have the following:

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();
    }
}

Sadly windows dies (blue screen). Is there any way I could close all the processes for the active use then hopefully Windows may survive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亽野灬性zι浪 2024-10-25 14:35:23

首先,在公共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...

西瑶 2024-10-25 14:35:23

确保安全的最简单方法是保留应用程序启动时存在的进程的列表,并将其排除在终止之外。但是,根据应用程序启动的时间(例如,如果它不是系统启动的一部分),这可能会让某些应用程序漏掉。从好的方面来说,它将允许设备的用户模式挂钩(例如鼠标和键盘处理程序)继续运行,这意味着在您杀死所有内容后系统仍然可用。

您可以尝试广播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.

淡看悲欢离合 2024-10-25 14:35:23
if (p.Id != me.Id
    && !p.ProcessName.ToLower().StartsWith("winlogon")
    && !p.ProcessName.ToLower().StartsWith("system idle process")
    && !p.ProcessName.ToLower().StartsWith("taskmgr")
    && !p.ProcessName.ToLower().StartsWith("spoolsv")
    && !p.ProcessName.ToLower().StartsWith("csrss")
    && !p.ProcessName.ToLower().StartsWith("smss")
    && !p.ProcessName.ToLower().StartsWith("svchost")
    && !p.ProcessName.ToLower().StartsWith("services")
    && !p.ProcessName.ToLower().StartsWith("lsass")
)
{
    if (p.MainWindowHandle != IntPtr.Zero)
    {
        p.Kill();
    }                          
}

这似乎有效。

if (p.Id != me.Id
    && !p.ProcessName.ToLower().StartsWith("winlogon")
    && !p.ProcessName.ToLower().StartsWith("system idle process")
    && !p.ProcessName.ToLower().StartsWith("taskmgr")
    && !p.ProcessName.ToLower().StartsWith("spoolsv")
    && !p.ProcessName.ToLower().StartsWith("csrss")
    && !p.ProcessName.ToLower().StartsWith("smss")
    && !p.ProcessName.ToLower().StartsWith("svchost")
    && !p.ProcessName.ToLower().StartsWith("services")
    && !p.ProcessName.ToLower().StartsWith("lsass")
)
{
    if (p.MainWindowHandle != IntPtr.Zero)
    {
        p.Kill();
    }                          
}

This seems to be working.

初懵 2024-10-25 14:35:23

您显然正在杀死窗口运行所需的重要进程。如果你杀死它们,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文