How does the task manager kill my program?

I have this MFC program that when I kill it using the task manager, I get an exception in my program and then it crashes.

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

I understand that there are several methods that the task manager uses to kill the process.

1) On the Applications tab, someone told me that he is sending a WM_CLOSE message to the main visible window of my application. And if my application does not work after a few seconds, the task manager will detect it as not responding and use TerminateProcess () about its process.

2) On the process tab, someone told me that he is using the TerminateProcess () window API.

Is there any other method that uses task manager?

How can I say about the last two methods?

Thanks in advance.

+7
source share
4 answers

Yes, both of them are true. You must answer WM_CLOSE to close gracefully. This can happen from anywhere, not just the task manager (e.g. shutdown).

MFC usually handles WM_CLOSE. If your application does not respond, then your main thread should sit doing something else, or, most likely, from your description, a failure somewhere in the WM_CLOSE handler.

Can you debug your application to find where the exception occurs?

+8
source

Yes, these are the parameters.

For completeness, note that applications in console mode receive a CTRL_CLOSE_EVENT message, which you can respond to when the "End Task" button is clicked.

Please note that you cannot intercept or respond to TerminateProcess . Your process will die, and you cannot do anything before this happens. Actually, that would be pretty bad if you could . Because then there would be no way to terminate the process that haywire would go.

+3
source

When you receive WM_CLOSE, you can easily find that your application can act on it.

I do not find it possible to find out when TerminateProcess is called to kill your application. The TerminateProcess documentation speaks of an immediate and unconditional termination of the target process.

(Depending on how much you want to achieve this, see this link on connecting to the Windows API, but don't expect it to be easy.)

+1
source

Task Manager uses EndTask . These functions send the WM_CLOSE message to your application. If your application does not respond to this message, and the user forces you to terminate your application, TerminateProcess is called in your process.

+1
source

All Articles