7

What is the difference between killing an application using the close button and ending the process from the Task Manager?

I am aware of the fact that hitting the close button posts a WM_CLOSE message in the message queue, but I don't know what happens when we kill a process from Task Manager (or any similar application like Killbox or Process Explorer).

user1232138
  • 5,451
  • 8
  • 37
  • 64

3 Answers3

7

When you click the "X" button in the title bar of an application's window, that sends the window a WM_CLOSE message. This is a "graceful" shutdown—the application processes the message, handles any necessary cleanup tasks, and can even refuse to shut down if it so desires (by returning zero in response to the message). WM_CLOSE is simply a request that the window or application terminate; the window is not destroyed until the application itself calls the DestroyWindow function.

When you press the "End Task" button in Task Manager, Windows will first try to send the application (if it is a GUI application) a WM_CLOSE message. In other words, it first asks nicely and gives the app a chance to terminate itself cleanly.*

If you fail to close in response to that initial WM_CLOSE message, the Task Manager will follow up by calling the TerminateProcess function. This function is a little bit different because it forcefully terminates the application's process and all of its threads without asking for permission from the app. This is a very harsh method of closing something, and should be used as a last resort—such as when an application is hung and is no longer responding to messages.

TerminateProcess is a very low-level function that essentially rips the user-mode part of a process from memory, forcing it to terminate unconditionally. Calling TerminateProcess bypasses such niceties as close notifications and DLL_PROCESS_DETACH. Your application does not have the ability to refuse to close, and there is no way to catch/trap/hook calls to TerminateProcess. All user-mode code in the process simply stops running for good. This is a very unclean shut down procedure, somewhat akin to jerking the computer's power plug out of the wall.

* Note that this only true if you use the "Applications" tab of Task Manager to kill the application. If you use the "Processes" tab, this step is skipped and the TerminateProcess function is called immediately. This distinction is reflected in the caption on the respective buttons. For the "Applications" tab, the button is lableled "End Task"; for the "Processes" tab, the button is labeled "End Process".

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I am trying to create another instance (detached process) of the same program while handling the WM_CLOSE message by using CreateProcess such that the program restarts itself, but it doesn't seem to be happening.. – user1232138 May 26 '12 at 10:09
  • @user What does that have to do with the question you asked here? And there's not enough information provided in your comment to debug the problem. What code do you have? What do you mean when you says "it doesn't seem to be happening"? What happens instead? – Cody Gray - on strike May 26 '12 at 10:10
  • You said that "Your application does not have the ability to refuse to close, and there is no way to catch/trap/hook calls to TerminateProcess", but what if we don't set the `PROCESS_TERMINATE` access rights for our program?? Will it still be possible to terminate our program using `TerminateProcess`.. – user1232138 May 26 '12 at 10:53
  • @user Yes, potentially, but that's a permissions issue. You can always prevent the user from killing applications by using Group Policy. But once the user elevates and runs Task Manager as an Administrator, they can do whatever they want. – Cody Gray - on strike May 26 '12 at 10:57
  • @Cody: I think you've got a typo there, but I can't tell which way around. Did you mean to say that TerminateProcess was synchronous, or that it returns to the caller before the process has been completely terminated? – Harry Johnston May 26 '12 at 22:42
  • @Harry: Bah! Good catch. Indeed I do; it's actually asychronous, initiating termination and returning immediately. But that didn't need to be in there anyway. – Cody Gray - on strike May 27 '12 at 04:53
  • A bit of an aside, but can you explain the difference, if any, between `TerminateProcess()` and the .NET method `System.Diagnostics.Process.Kill()`? – Conrad Dec 16 '14 at 00:20
4

Killing the process with WM_CLOSE simply signals the process with the message and allows the target to handle the message and exit gracefully. Alternatively, the process could choose not to exit in its WM_CLOSE handler.

Killing the process via task manager will do so with TerminateProcess which is far harsher:

The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.

This function stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed or canceled. When a process terminates, its kernel object is not destroyed until all processes that have open handles to the process have released those handles.

TerminateProcess is asynchronous; it initiates termination and returns immediately. If you need to be sure the process has terminated, call the WaitForSingleObject function with a handle to the process. A process cannot prevent itself from being terminated.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
3

If you close an application using the close button you let the application to perform necessary closing tasks if any. If you kill a process from task manager there is no chance for application to perform those tasks, you just terminate the application without informing.