moses-palmer / pystray

GNU General Public License v3.0
463 stars 57 forks source link

Feature request: execute cleanup code when the app quits #78

Closed julien-h closed 3 years ago

julien-h commented 3 years ago

Is there a way to execute cleanup code in the app before it is killed by Windows or quits? Including before logout, shutdown and restart.

On Windows operating system, console python applications receive some signals but apparently, not windowed ones.

moses-palmer commented 3 years ago

Does the atexit module not work on Windows?

julien-h commented 3 years ago

Yes and no, it's somewhat limited in what it can do:

The functions registered via this module are not called when the program is killed by a signal not handled by Python, when a Python fatal internal error is detected, or when os._exit() is called.

I found two useful links:

  1. https://stackoverflow.com/questions/40866576/run-atexit-when-python-process-is-killed
  2. https://stackoverflow.com/questions/1411186/python-windows-shutdown-events

It looks like the accepted answer to the second link provides a solution but I don't understand it.

moses-palmer commented 3 years ago

Thanks for the links.

I think that this is somewhat out of scope for this library however, as it is not related to system tray icons at all, so I am afraid that I will close this issue.