Handling end process of a windows app

后端 未结 6 1802
無奈伤痛
無奈伤痛 2020-12-01 06:49

Is it possible to capture the task manager end process of a windows application within the same windows application itself? I am using a C# 2.0 win app and I would like to d

6条回答
  •  日久生厌
    2020-12-01 07:28

    No, it is not possible to hook the operating system's decision to end a process. Note, this is not done by task manger, ending a process is the responsibility of the kernel.

    You will need to do two things here:

    1. Connect event handlers to the normal user interface messages that tell a application to exit. Use these events to persist data, free resources, and otherwise exit cleanly.
    2. Handle exceptions as appropriate to catch errors and clean up and save data if possible.

    Here are a three links to Raymond's blog explaining why you cannot do what you are asking.

    • Why can't you trap TerminateProcess?
    • Why do some process stay in Task Manager after they've been killed?
    • The arms race between programs and users

    Also, I addressed a similar StackOverflow question here.

提交回复
热议问题