Skip to content

Latest commit

 

History

History
8 lines (4 loc) · 942 Bytes

README.md

File metadata and controls

8 lines (4 loc) · 942 Bytes

win_graceful_shutdown

A python3 library for handling graceful shutdown in windows in response to: CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, WM_CLOSE, and WM_ENDSESSION

Python has built-in support for exit handlers though the atexit standard library. The problem is, it's pretty hard to get those exit handlers to run reliably on windows. The windows console might close your program via CTRL_C_EVENT or CTRL_BREAK_EVENT, and those are pretty easy to catch and handle. But It might also close your program via CTRL_CLOSE_EVENT, which is a bit more complicated to handle, but not much. Where it starts to get hairy is trying to handle WM_CLOSE, which is what taskkill.exe sends, or WM_ENDSESSION which is what windows sends when it is shutting down or logging off.

This handles all of those situations with just an import. Simply import win_graceful_shutdown and your exit handlers are guaranteed to fire when the program closes.