Click here to Skip to main content
15,845,681 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have created one windows form application, I need to write a log file on application exit. I used the form closing event, but if the application is closed using task manager or logoff then the coding is not working. Can any body have a solution for this?.

1 solution

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[^]

Source: Handling end process of a windows app[^]
Share this answer
TheRealSteveJudge 24-Apr-15 6:09am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900