5

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.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
Sally
  • 1,749
  • 7
  • 31
  • 39
  • 16
    Step away from the keyboard... – David M Feb 21 '11 at 22:21
  • 1
    Why do you want to kill all those processes? – Rohan West Feb 21 '11 at 22:21
  • 1
    Can you explain what you're trying to achieve? I start running this app, then I open Word or FireFox or something, I leave the app neglected, it waits 15 min and then closes Word/FireFox whatever to get me to focus on my work? – Kate Gregory Feb 21 '11 at 22:22
  • 2
    sounds like "annoying virus" code :) – jb. Feb 21 '11 at 22:24
  • What user context is the program running in? Will the user log on and run only this app or is this app executed in a SYSTEM context? ( This sounds to me something like a kiosk app.) – hova Feb 21 '11 at 22:26
  • @KateGregory exactly how it should work. It will send you a nice message where you can cancel the shut down. – Sally Feb 21 '11 at 22:28
  • @RohanWest this is a public PC that can be used by guests. I need to make sure after someone has used the machine it is at a clean state for the next user – Sally Feb 21 '11 at 22:29
  • @jb no virus and I did mark it as dangerous-request :) – Sally Feb 21 '11 at 22:30
  • @hova will run as a sytem process but at the moment is running as a user. – Sally Feb 21 '11 at 22:30
  • 1
    @DavidM If I step away can you help it that all the processes close after 15mins without Windows crashing. Otherwise I need to stay here :( – Sally Feb 21 '11 at 22:31
  • Well, if it is running as SYSTEM, why not just have it search for the user attached to the console, and then log them off? (This will be more difficult than it sounds) – hova Feb 21 '11 at 22:36
  • Hi Sally. I'm curious, what's the `ProcessName` of your application? – Dour High Arch Feb 21 '11 at 22:44
  • First you should figure out what processes would be safe (in the sense of _not_ crashing the OS) to kill. That's not something you should be asking us to do for you. – Jeff Mercado Feb 21 '11 at 22:56
  • 1
    i have in past tried end shutting down many process in manner.. DO NOT TRY!!! be of full list items of which termination may be possible, iterate – PRASHANT P Feb 21 '11 at 23:27
  • 2
    When you are on Windows 7 (or Vista) the solution would be very simple: Don't run your application with elevated rights (given that your application is not a service). – Dirk Vollmar Feb 21 '11 at 23:35

4 Answers4

4

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

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
3

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.

Zooba
  • 11,221
  • 3
  • 37
  • 40
  • anwer leads of indication for problem solution of any cost.. many times corrected answers put wrong problem into hands, NO SOLUTION!! look to put out new idea before answer maybe :) – PRASHANT P Feb 21 '11 at 23:30
2
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.

Sally
  • 1,749
  • 7
  • 31
  • 39
1

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.

Femaref
  • 60,705
  • 7
  • 138
  • 176