0

This might sound odd, but I need to find a solution to kill the process my tool is running under from the code. I can kill all the processes having the name:

foreach (Process process in Process.GetProcessesByName("name_of_my_tool"))
{
    process.Kill();
}

But there might be multiple instances of the tool, and I only want to kill the current one.

So can I somehow get the id, and kill the process by it? Basically I need a suicide function.

fishmong3r
  • 1,414
  • 4
  • 24
  • 51
  • 2
    Environment.Exit() is probably what you need. – Zache Jun 02 '14 at 14:19
  • 1
    See: http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application – Shelakel Jun 02 '14 at 14:19
  • 1
    Do I assume you've already tried Process.GetCurrentProcess() and had a problem with it? – Bob Moore Jun 02 '14 at 14:19
  • Why not just use Environment.Exit? Alternatively if you have a good reason to use the Process namespace, you could use Process.GetCurrentProcess().Kill(). – NeilMacMullen Jun 02 '14 at 14:22
  • What is the difference between `Environment.Exit()` and `Process.GetCurrentProcess().Kill()`? – fishmong3r Jun 02 '14 at 14:23
  • 1
    Read the linked question to understand it. If you want suicide as in KILL ME RIGHT NOW NOW NOW there is also ``Environment.FailFast`` – Zache Jun 02 '14 at 14:26

1 Answers1

1

What's wrong with this simple snippet?

Process.GetCurrentProcess();
amiry jd
  • 27,021
  • 30
  • 116
  • 215