Killing the Processes Using process.Kill() restarts the system
-
When I run the following code from inside the Visual Studio, it works fine, but when I run the exe, it restarts my machine. I have written the following code: System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses(); foreach (System.Diagnostics.Process proces in prs) { try { if (proces.ProcessName.ToLower().ToString().Trim() != "" && proces.ProcessName.ToLower().ToString().Trim() != "manageallprocess.vshost" && proces.ProcessName.ToString().Trim() != "ManageAllProcess" && proces.ProcessName.ToLower().ToString().Trim() != "devenv" && proces.ProcessName.ToUpper().ToString().Trim() != "EXPLORER" && proces.ProcessName.ToString().Trim() != "Explorer" && proces.ProcessName.ToLower().ToString().Trim() != "explorer" && proces.ProcessName.ToLower().ToString().Trim() != "system" && proces.ProcessName.ToLower().ToString().Trim() != "idle" && proces.ProcessName.ToLower().ToString().Trim() != "svchost" && proces.ProcessName.ToUpper().ToString().Trim() != "SVCHOST" && proces.ProcessName.ToString().Trim() != "SvcHost") { proces.Refresh(); if (!proces.HasExited) { proces.Kill(); proces.WaitForExit(); } } } catch (Exception exc) { }
-----Have A Nice Day-----
Umh, killing svchost on Windows is never a good idea. Besides that, why don't you call
proces.ProcessName.ToUpper().ToString().Trim()
just once for each process in the list? Also, comparing something not in uppercase to this string will always result infalse
, so the!=
comparison is pointless. -
I think you need a bit more experience before you start killing processes!
Neeraj Kr wrote:
proces.ProcessName.ToLower().ToString().Trim() != "svchost" && proces.ProcessName.ToUpper().ToString().Trim() != "SVCHOST" && proces.ProcessName.ToString().Trim() != "SvcHost")
all do the same thing - you only need one of them More worryingly, and the cause of your problem I expect, is the fact that unless the process is in your list it will be killed. This will kill all sorts of system processes by the look of it.
Bob Ashfield Consultants Ltd
Yea, that is why I am putting a condition on SVCHOST and the application name through which I am running the kill process. Also, I have put a condition that all the explorer windows that are open do not get closed. What do you suggest???
-----Have A Nice Day-----
-
Umh, killing svchost on Windows is never a good idea. Besides that, why don't you call
proces.ProcessName.ToUpper().ToString().Trim()
just once for each process in the list? Also, comparing something not in uppercase to this string will always result infalse
, so the!=
comparison is pointless.yea, I know that killing the svchost on windows is not good that is why I have put a check on that. Though, I guess could have written one condition for one process and not three different. What else do you suggest please. The problem is that when I run the application from VS 2008, it runs fine, but when I run the exe, it reboots my system.
-----Have A Nice Day-----
-
Yea, that is why I am putting a condition on SVCHOST and the application name through which I am running the kill process. Also, I have put a condition that all the explorer windows that are open do not get closed. What do you suggest???
-----Have A Nice Day-----
Neeraj Kr wrote:
What do you suggest???
What exactly are you trying to achieve? To me it looks like your code is trying to kill all running processes except Explorer, SvcHost and your own app. Why do you want to do this?
Simon
-
Neeraj Kr wrote:
What do you suggest???
What exactly are you trying to achieve? To me it looks like your code is trying to kill all running processes except Explorer, SvcHost and your own app. Why do you want to do this?
Simon
-
Neeraj Kr wrote:
What do you suggest???
What exactly are you trying to achieve? To me it looks like your code is trying to kill all running processes except Explorer, SvcHost and your own app. Why do you want to do this?
Simon
-
Umh, killing svchost on Windows is never a good idea. Besides that, why don't you call
proces.ProcessName.ToUpper().ToString().Trim()
just once for each process in the list? Also, comparing something not in uppercase to this string will always result infalse
, so the!=
comparison is pointless.Greeeg wrote:
proces.ProcessName.ToUpper().ToString().Trim() just once for each process in the list
Thats exactly what I meant when I said
Neeraj Kr wrote:
proces.ProcessName.ToLower().ToString().Trim() != "svchost" && proces.ProcessName.ToUpper().ToString().Trim() != "SVCHOST" && proces.ProcessName.ToString().Trim() != "SvcHost") all do the same thing - you only need one of them
And it seems to me that anything other than those in his list will be killed, hence the system reboot.
Bob Ashfield Consultants Ltd
-
yea, I know that killing the svchost on windows is not good that is why I have put a check on that. Though, I guess could have written one condition for one process and not three different. What else do you suggest please. The problem is that when I run the application from VS 2008, it runs fine, but when I run the exe, it reboots my system.
-----Have A Nice Day-----
Assuming it is related to your process stomping, put a confirm dialog ahead of each kill and wait a minute or so between each yes click. That'll let you know what process's death is taking the system down. The requirement itself is a major WTF, there has to be a better way to accomplish whatever the clients actual objective is.
You know, every time I tried to win a bar-bet about being able to count to 1000 using my fingers I always got punched out when I reached 4.... -- El Corazon
-
Simon Stevens wrote:
To me it looks like your code is trying to kill all running processes except Explorer, SvcHost and your own app
Exactly. This is what I want to achieve. This is a customer requirement.
-----Have A Nice Day-----
Neeraj Kr wrote:
Exactly. This is what I want to achieve. This is a customer requirement.
This is a stupid customer requirement. Killing all processes will always restart the PC because you will be killing critical OS processes. (The only reason it doesn't when you run from inside visual studio is because VS is preventing your app from doing stupid things) Tell your customer that they don't really want to do that. Ask them what they are trying to achieve by killing all processes.
Simon
-
Neeraj Kr wrote:
Exactly. This is what I want to achieve. This is a customer requirement.
This is a stupid customer requirement. Killing all processes will always restart the PC because you will be killing critical OS processes. (The only reason it doesn't when you run from inside visual studio is because VS is preventing your app from doing stupid things) Tell your customer that they don't really want to do that. Ask them what they are trying to achieve by killing all processes.
Simon
-
ha ha ha. Actually they already have a third party tool which does the same thing, but does not restarts the system. They actually want a proprietory product.
-----Have A Nice Day-----
What the third party app will be doing is only killing non-critical processes. Find out the real requirements from your customer. They don't really want to kill all processes, because that will cause a system restart. Find out what they actually want. I suspect they want something like, close all open applications. In which case, you need to find a way of only closing user apps and not just blindly killing all system processes. You should probably be using process.Close() instead of kill. This gives the exiting processes a chance to clean up their data. Either way, the important point here is to ask the client what they actually want and why they want to do it.
Simon
-
Greeeg wrote:
proces.ProcessName.ToUpper().ToString().Trim() just once for each process in the list
Thats exactly what I meant when I said
Neeraj Kr wrote:
proces.ProcessName.ToLower().ToString().Trim() != "svchost" && proces.ProcessName.ToUpper().ToString().Trim() != "SVCHOST" && proces.ProcessName.ToString().Trim() != "SvcHost") all do the same thing - you only need one of them
And it seems to me that anything other than those in his list will be killed, hence the system reboot.
Bob Ashfield Consultants Ltd
-
Assuming it is related to your process stomping, put a confirm dialog ahead of each kill and wait a minute or so between each yes click. That'll let you know what process's death is taking the system down. The requirement itself is a major WTF, there has to be a better way to accomplish whatever the clients actual objective is.
You know, every time I tried to win a bar-bet about being able to count to 1000 using my fingers I always got punched out when I reached 4.... -- El Corazon
-
Ashfield wrote:
And it seems to me that anything other than those in his list will be killed
Which specific process causes the system to reboot apart from the SVCHOST. We can restrict the same.
-----Have A Nice Day-----
Countless ones! wininit.exe for example, winlogin too, csrss, services (and a host of services required under that). Open you Task Manager, and start killing them and you will see. If you wanna do things in this retarded way, you will need to suffer the pain of debugging it.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)