Reputation: 137
Could anyone tell me how to ensure some code in my program gets executed when its process exits or gets killed?
The destroyer only gets called when it exits normally. But I want my code get executed when it's killed by a system shutdown or task manager.
The platform is Windows and I don't mind using platform specific code if it's needed.
Upvotes: 1
Views: 400
Reputation: 59997
Assuming that the program is crashing in a more-or-less control way perhaps atexit is the best bet
Upvotes: 1
Reputation: 107247
Ending a task from Task Manager or from a Windows shutdown will both give the process a chance to shutdown gracefully by sending messages such as WM_CLOSE
, which you can use to close down your process gracefully. However, as per @immibis answer, there are scenarios where this cannot be guaranteed.
Upvotes: 1
Reputation: 58868
You can't. What if the user pulls out the power cord?
Upvotes: 5