8

I have this MFC program that when I kill it with task manager I get an exception on my program and then it crashes.

I want to get the event from the task manager, when it is going to kill my process and close my program gracefully.

I understand that there are few methods that the task manager is using in order to kill a process.

1) From the applications tab, someone told me it is sending WM_CLOSE message to my application's main visible window, And if my application is not going down after few seconds, the task manager detects it as not-responding and uses TerminateProcess() on its process.

2) From the process tab, someone told me it is using TerminateProcess() windows API.

Is there any other method the Task Manager is using?

Am i right about the last 2 methods?

Thank you in advance.

Vic
  • 21,473
  • 11
  • 76
  • 97
eladyanai
  • 1,063
  • 2
  • 15
  • 34

4 Answers4

8

Yes, both of these are correct. You should respond to WM_CLOSE to close gracefully. This could come from anywhere, not just task manager (shutdown for instance).

MFC normally handles WM_CLOSE. If your app is not responding then your main thread must be sat doing something else, or more likely from your description is crashing somewhere in the WM_CLOSE handler.

Can you debug your app to find where the exception is being raised?

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • 1
    thank you, i really didn't respond to WM_CLOSE message that was sent by the Task Manager. I have found an EMPTY implementation of `afx_msg void OnClose();` all i had to do is call the base class's implementation of OnClose. – eladyanai Feb 28 '12 at 14:52
3

Yes, these are the options.

For completeness, note that console mode applications get the CTRL_CLOSE_EVENT send that you could react upon, when the "End Task" button is clicked.

Note that you cannot intercept or react upon TerminateProcess. Your process will die and there is nothing you can do before that happens. Actually, it would be pretty bad if you could. Because then there would be no way to terminate a process that went haywire.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • very good to know! thank you very much, and i agree about the TerminateProcess explanation. – eladyanai Feb 28 '12 at 09:00
  • It's worth to add that killing the process from Task Manager in "Details" tab will just do `TerminateProcess` without possibility to handle `CTRL_CLOSE_EVENT`. If you do "End task" from "Process -> Apps" tab, ensure to do it on the root of the process tree, otherwise you may similarly be not able to handle the event. – ceztko Sep 17 '18 at 10:45
2

Task manager internally uses the EndTask function. This functions sends a WM_CLOSE message to your application. If your application does not respond to that message and the user forces to terminate your Application, TerminateProcess is called on your process.

Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33
1

When you get a WM_CLOSE, you can easily detect that so your application can act upon it.

I don't believe it's possible to know when TerminateProcess is being called to kill your application. The TerminateProcess documentation says it's an immediate and unconditional shutdown of the target process.

(Depending on how much you want to achieve this, take a look at this link about hooking into the Windows API but don't expect it to be easy.)

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • thank you for your answer, actually, i didn't respond to WM_CLOSE that was sent by the Task Manager- when killing my process from the Applications tab. killing my process from the process tab (by TerminateProcess function) didn't make my application to crash. – eladyanai Feb 28 '12 at 08:58