How does task manager kill my program?

后端 未结 4 611
北恋
北恋 2021-01-18 00:22

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, wh

相关标签:
4条回答
  • 2021-01-18 00:56

    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?

    0 讨论(0)
  • 2021-01-18 00:58

    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.)

    0 讨论(0)
  • 2021-01-18 01:05

    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.

    0 讨论(0)
  • 2021-01-18 01:09

    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.

    0 讨论(0)
提交回复
热议问题