C# Kill all processes not essential to running Windows

4.7k views Asked by At

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.

4

There are 4 answers

0
Sally On BEST ANSWER
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.

1
Femaref On

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.

1
Zooba On

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.

0
Stephen Cleary On

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...