Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Killing the Processes Using process.Kill() restarts the system

Killing the Processes Using process.Kill() restarts the system

Scheduled Pinned Locked Moved C#
csharpvisual-studio
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Neeraj Kr

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

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    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 in false, so the != comparison is pointless.

    N A 2 Replies Last reply
    0
    • A Ashfield

      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

      N Offline
      N Offline
      Neeraj Kr
      wrote on last edited by
      #4

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

      S 1 Reply Last reply
      0
      • L Lost User

        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 in false, so the != comparison is pointless.

        N Offline
        N Offline
        Neeraj Kr
        wrote on last edited by
        #5

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

        D 1 Reply Last reply
        0
        • N Neeraj Kr

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

          S Offline
          S Offline
          Simon P Stevens
          wrote on last edited by
          #6

          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

          N 2 Replies Last reply
          0
          • S Simon P Stevens

            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

            N Offline
            N Offline
            Neeraj Kr
            wrote on last edited by
            #7

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

            1 Reply Last reply
            0
            • S Simon P Stevens

              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

              N Offline
              N Offline
              Neeraj Kr
              wrote on last edited by
              #8

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

              S 1 Reply Last reply
              0
              • L Lost User

                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 in false, so the != comparison is pointless.

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #9

                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

                N 1 Reply Last reply
                0
                • N Neeraj Kr

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

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #10

                  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

                  N 1 Reply Last reply
                  0
                  • N Neeraj Kr

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

                    S Offline
                    S Offline
                    Simon P Stevens
                    wrote on last edited by
                    #11

                    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

                    N 1 Reply Last reply
                    0
                    • S Simon P Stevens

                      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

                      N Offline
                      N Offline
                      Neeraj Kr
                      wrote on last edited by
                      #12

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

                      S 1 Reply Last reply
                      0
                      • N Neeraj Kr

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

                        S Offline
                        S Offline
                        Simon P Stevens
                        wrote on last edited by
                        #13

                        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

                        1 Reply Last reply
                        0
                        • A Ashfield

                          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

                          N Offline
                          N Offline
                          Neeraj Kr
                          wrote on last edited by
                          #14

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

                          L 1 Reply Last reply
                          0
                          • D Dan Neely

                            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

                            N Offline
                            N Offline
                            Neeraj Kr
                            wrote on last edited by
                            #15

                            Let me just try that. Though I tried to put a MessageBox, but that was of no help.

                            -----Have A Nice Day-----

                            1 Reply Last reply
                            0
                            • N Neeraj Kr

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

                              L Offline
                              L Offline
                              leppie
                              wrote on last edited by
                              #16

                              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)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups