3

I am having a bit of trouble trying to terminate a process, I realize there is a fair amount of recourses on this site alone, but I was wondering if there's any alternative ways of terminating an application rather than something typical such as:

    Process[] procs = Process.GetProcessesByName("test");
    foreach (Process proc in procs)
        proc.Kill(); 
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
James
  • 1,459
  • 3
  • 16
  • 15
  • 9
    You can pull the power-cable. – H H Jul 16 '11 at 20:41
  • @Henk Holterman lol +1 if I could vote :P – James Jul 16 '11 at 20:43
  • 4
    Why do you need an alternate way, does not `Process.Kill` work? – Lasse V. Karlsen Jul 16 '11 at 21:17
  • The framework really needs a method for closing window-less processes gracefully, because Kill and CloseMainWIndow are useless when I'm batch starting 3rd party console apps in the background. Kill leaves incomplete files hanging around because it's too abrupt, while CloseMainWindow either returns false and does nothing or throws an error if the process already completed and closed on its own. The console apps (7-zip and innumerable other ones) were designed to respond to ctrl+c or ctrl+break signals, but there's no way to send those single to an arbitrary process started by my application. – Triynko Apr 19 '13 at 22:25
  • @Triynko: Are you sure that "there's no way to send those signals"? Did you try the answers suggested in http://stackoverflow.com/q/283128/87698? – Heinzi Apr 21 '13 at 16:34
  • I'm sure there is no way to do it in the .NET framework. The `Process` class lacks the methods to do so, since Kill and CloseMainWindow don't work. The solutions in the thread you posted don't work. One of them has a score of negative 5 for a reason... it doesn't work at all for a process started with a separate process group with no console window. GenerateConsoleCtrlEvent doesn't work. The accepted answer, closing the standard input, states in the comment that "it only works if the process is trying to read from standard input", and won't work since 7-zip isn't trying to read the input. – Triynko Apr 22 '13 at 03:09

4 Answers4

2

There's Process.CloseMainWindow, which nicely asks the process to quit (as opposed to Process.Kill, which shoots down the process and can have negative side effects).

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 1
    Unfortunately, in this case, this kind of approach wouldn't be suitable :( – James Jul 16 '11 at 20:46
  • 9
    @James: I see. To increase the probability of useful answers, I would recommend you to edit your question and add (1) why CloseMainWindow (which is the recommended way to close other applications) is unsuitable and (2) what problems you are currently facing when using Process.Kill. When suggesting alternatives, it's always good to know *why* the current solution is not good. – Heinzi Jul 16 '11 at 20:48
  • 1
    This simply doesn't work when Process.ProcessStartInfo.CreateNoWindow is true. CloseMainWindow just returns false and nothing happens, so this is useless for situations where one is batch starting background processes, such as console applications, without individual windows. What we really need is a functional way to send a control message to a process, because GenerateConsoleCtrlEvent (http://msdn.microsoft.com/en-us/library/windows/desktop/ms683155(v=vs.85).aspx) can't send the message to an arbitrary process and has all kinds of other limitations that make it useless as well. – Triynko Apr 19 '13 at 21:38
2

There are only 2 ways in C# to close the Process (AFAIK) using Process.Kill() and Process.CloseMainWindow(), Kill sends an immediate KILL signal to the application and forces it to close immediately. CloseMainWindow uses SendWindowMessage to send a CLOSE signal to the main application. Kill can be unsafe because it immediately stops the process. CloseMainWindow can be followed by Process.WaitForExit so that you can be sure that the application has closed and may continue to do work knowing that the process you told to exit has exited correctly. As posted by Heinzi's comment please be a little more specific I'm just trying to expand on what was said in the hopes that this is what you require.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

Very simple, just need to get the process name and kill it, don't try to do anything fancy, sometimes less is more...

Process[] prs = Process.GetProcesses();


foreach (Process pr in prs)
{
if (pr.ProcessName == "test")
{

pr.Kill();

}

}
TGarrett
  • 562
  • 4
  • 15
-1

This idea is not good. There could be another running process(es) with that name. Do you want any process with that name to be terminated? Unless you are writing a Task Manager/Process Explorer kind of application, you should never do that. And even with TM kind of application, you close the process by grabbing its handle/Process object, and not by name.

Thy can't you ask the target process to close itself gracefully? May be you can use a named mutex, the target thread would wait on that mutex. When you signal that named-mutex from another process, the target thread would know it is time to exit and eventually exit.

Ajay
  • 18,086
  • 12
  • 59
  • 105