3

I am working on a c# winforms application. I am trying to close a running process by its process ID.

try
{
  //Find process & Kill
  foreach (Process Proc in (from p in Process.GetProcesses()
                            where p.ProcessName == "taskmgr" || p.ProcessName == "explorer"
                            select p))
  {
    Microsoft.VisualBasic.Interaction.Shell("TASKKILL /F /IM " + Proc.ProcessName + ".exe");
  }
}
catch (Exception ex)
{
  ErrorLogging.WriteErrorLog(ex);
}
return null;

This code is not working on windows 2003 SP 2. I have googled and found that 2003 does not have the taskkill command. What would be a substitute for that?

11
  • 3
    Why use command line instead of a built in function for exiting? Commented Aug 4, 2014 at 4:27
  • I have some restriction due to security purpose so i am using this way Commented Aug 4, 2014 at 4:28
  • 5
    What is the security restriction? I can't think of anything that would prevent an application from closing itself. It sounds like you're attempting a poor workaround instead of addressing your real issue. Commented Aug 4, 2014 at 4:34
  • @mason i am developing a interface so called secure browser where another online exam project going to held at that time i am not allowing candidate to press and key or switching application or anything else. This secure browser will be stand alone at the time of exam. So in this case i am going to restrict all the application open in task bar . Commented Aug 4, 2014 at 4:47
  • 1
    That still doesn't explain why you can't terminate the execution using built in C# function. There is no need to resort to command line commands for the situation you just described. Commented Aug 4, 2014 at 4:49

4 Answers 4

4

Use the Process.Kill method. If you have the required permission it will work.

Process.Kill Method Immediately stops the associated process.

Sample

try
{
    //Find process
    foreach (Process Proc in (from p in Process.GetProcesses()
                                where p.ProcessName == "taskmgr" ||
                                    p.ProcessName == "explorer"
                                select p))
    {
        // "Kill" the process
        Proc.Kill();
    }
}
catch (Win32Exception ex)
{
    // The associated process could not be terminated.
    // or The process is terminating.
    // or The associated process is a Win16 executable.
    ErrorLogging.WriteErrorLog(ex);
}
catch (NotSupportedException ex)
{
    // You are attempting to call Kill for a process that is running on a remote computer. 
    // The method is available only for processes running on the local computer.
    ErrorLogging.WriteErrorLog(ex);
}
catch (InvalidOperationException ex)
{
    // The process has already exited.
    // or There is no process associated with this Process object.
    ErrorLogging.WriteErrorLog(ex);
}
return null;

More Information

Sign up to request clarification or add additional context in comments.

4 Comments

Instead of catching Exception you could just catch Win32Exception and InvalidOperationException. Because GetProcesses won't throw anything and Kill won't throw a NotSupportedException since the procs are guaranteed to be local. Reference: msdn.microsoft.com/en-us/library/…
Not trying to nitpick here, but the point of my comment was to point out that catching Exception is generally not the best idea if you can catch more specific exceptions. Right now, you're still catching everything that derives from Exception which includes things like an OutOfMemoryException. That is something you should not be handling at that point. Thus, I think you should have two catch clauses, one for the Win32Exception and one for the InvalidOperationException.
I catch the exception that can occur in Kill method according to the msdn documentation. Off course i can use a catch clause for everything but then i got redundancy (calls of ErrorLogging.WriteErrorLog(ex);)
No, you're simply catching everything, not just what is documented on MSDN. I think it's better design to catch exactly what you can actually handle rather than catching everything to prevent some code redundancies (stackoverflow.com/questions/4673860/…). Plus, what's supposed to be in the body of the IF's? Will that not result in code redundancy?
0

Get the current process and use: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx Process.Kill to kill the corresponding process.

Comments

0

Find the window associated with the process & send WM_CLOSE message to it.

Comments

0

If you don't want to see the cmd windows anymore you can use:

proc.Kill();
proc.CloseMainWindow();

of course this is useful when you have used:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();    
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
proc.StartInfo = procStartInfo;
proc.Start();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.