locked
Kill process RRS feed

  • Question

  • Hello people,

    Obviously it isnt that hard to kill a process however it seems to work only once even though I have placed it in a while (true) loop:

    while (true)
                   {
                       foreach (Process clsProcess in Process.GetProcesses())
                       {
                           if (clsProcess.ProcessName.StartsWith("randomprocerss"))
                               clsProcess.Kill();

                       }

                   }

    So why exactly does it work only once?

    Regards.

    Wednesday, October 6, 2010 9:19 AM

Answers

  • hi,
    The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.
    For more details please refer to this link: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(VS.90).aspx

    Also check this link which is performing similar task: http://www.devasp.net/net/articles/display/717.html

    There is also one helpful property which can trap exceptions or error thrown by process: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror(VS.85).aspx

    Hope this helps you.


    Manish Patil http://patilmanishrao.wordpress.com Posting is provided AS IS with no warranties, and confers no rights.
    • Proposed as answer by Manishrao Patil Tuesday, October 12, 2010 5:27 AM
    • Marked as answer by Alan_chen Wednesday, October 13, 2010 2:14 AM
    Wednesday, October 6, 2010 12:31 PM
  • Look like process information is being cahed. You can force to get new values it may solve this problem.

    call Process.Refresh() after 'HasExited( )'

    After Refresh is called, the first request for information about each property causes the process component to obtain a new value from the associated process.

     

    • Marked as answer by Alan_chen Wednesday, October 13, 2010 2:15 AM
    Friday, October 8, 2010 7:50 AM
  • Hi Noxrawr,

    Welcome to MSDN forums!

    If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied.

    @Manishrao has supplied helpful resources, try to read the links, you can learn more from them.

     

    Hope these helps, if you have any problems, please feel free to let me know.

    Best Regards,
    Alan Chen
    ________________________________________
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

     

     

    • Marked as answer by Alan_chen Wednesday, October 13, 2010 2:15 AM
    Friday, October 8, 2010 6:49 AM

All replies

  • the second time it give you an exception or it doesn't kill the process?
    Wednesday, October 6, 2010 9:24 AM
  • It doesnt kill the process but since its between try {} tags it wont give an exception, I will remove that and see what happens.

    EDIT: removed the try tags and no exception seems to occur.

    Wednesday, October 6, 2010 9:29 AM
  • hi,
    The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.
    For more details please refer to this link: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(VS.90).aspx

    Also check this link which is performing similar task: http://www.devasp.net/net/articles/display/717.html

    There is also one helpful property which can trap exceptions or error thrown by process: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror(VS.85).aspx

    Hope this helps you.


    Manish Patil http://patilmanishrao.wordpress.com Posting is provided AS IS with no warranties, and confers no rights.
    • Proposed as answer by Manishrao Patil Tuesday, October 12, 2010 5:27 AM
    • Marked as answer by Alan_chen Wednesday, October 13, 2010 2:14 AM
    Wednesday, October 6, 2010 12:31 PM
  • Thank you for replying, I have followed your advice and the code now looks like this.
    ive found that testing this works very well with taskmgr because you can open it very quickly by simply holding ctrl alt delete and see how effective the code really is.

    while (true)
                 {
                   foreach (Process clsProcess in Process.GetProcesses())
                  {
                     
                       if (clsProcess.ProcessName.StartsWith("taskmgr"))
                           if (!clsProcess.HasExited)
                           { clsProcess.Kill(); }
                   
                   }

                 }

     

    Frankly, it works better than it did before, but sometimes it still fails and if you only just open up a particular process often and quickly enough its just a matter of tries before my application just seems to break from the while loop and stops functioning.
    If you have any ideas please feel most welcome to let me know.

    Regards.

    Thursday, October 7, 2010 9:16 AM
  • Hi Noxrawr,

    Welcome to MSDN forums!

    If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied.

    @Manishrao has supplied helpful resources, try to read the links, you can learn more from them.

     

    Hope these helps, if you have any problems, please feel free to let me know.

    Best Regards,
    Alan Chen
    ________________________________________
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

     

     

    • Marked as answer by Alan_chen Wednesday, October 13, 2010 2:15 AM
    Friday, October 8, 2010 6:49 AM
  • Look like process information is being cahed. You can force to get new values it may solve this problem.

    call Process.Refresh() after 'HasExited( )'

    After Refresh is called, the first request for information about each property causes the process component to obtain a new value from the associated process.

     

    • Marked as answer by Alan_chen Wednesday, October 13, 2010 2:15 AM
    Friday, October 8, 2010 7:50 AM
  • Hi,

    Congrats for having good grip on your code then before. But since there is some issue croping-in. Below are my suggetion:
    1. As Alan pointed it could be the possible reason for abnormal behaviour.
    2. From the code [specifically while(true){ }] I feel it keeps on running continously and cannot break the loop.
    If this is the case then I suggest please re-consider the code (try to avoid while(true){ }). And this could be the reason for point 1 above.
    3. Is this some form of window service / exe which keeps on running and checks for a particular process existance?
    if so then possibly the reason could be threading and or service control manager (these guys abnormally skips the rountine for processing other requests). To avoid this use timer control or other possible options.

    Again these are the possible suggestions as per my understanding and may not neccessarily suit to your system. Else please elaborate about the system/utility that performs this operation / task.

    Manish Patil http://patilmanishrao.wordpress.com Posting is provided AS IS with no warranties, and confers no rights.
    Friday, October 8, 2010 8:14 AM