On Fri, Aug 1, 2008 at 7:31 PM, Mark Hammond <firstname.lastname@example.org>
You will probably find this isn't specific to py2exe and it
behaves the same
when running under pythonw.exe - if that isn't true, you should probably
chase up why things are different rather than re-inventing your own. But
something like the following might help:
import win32api, win32con
# NOTE that this is called on its own thread
# False means "run the next/default handler", True means
rc = False
# During a log-off etc process the console is also closed, so this
# OK here.
if evt in (win32con.CTRL_CLOSE_EVENT, win32con.CTRL_LOGOFF_EVENT):
... do something to *asynchronously* close the app.
rc = True
Note that the above is untested and was pulled from an app that will either
have a console or a real window, so just uses win32con.CTRL_CLOSE_EVENT (and
relies on the window being closed at logoff) so you might need to experiment
with exactly which events you need to handle (and as I implied above, try
and find out if twisted is expected to handle this situation ok already, or
already has a "Console Control Handler" that needs tweaking, etc)
take care of threading issues - unlike a unix signal, this comes on a new
thread created just for the purpose of delivering the event, so you will
need to find out how to break your event loop externally.
On Behalf Of Mats
Sent: Friday, 1 August 2008 7:08 PM
Subject: [Py2exe-users] Executing code at shutdown (windows)
I have a windows app that behaves strangely on shutdown. Basically, the
is using twisted and uses twisted's reactor as the "event loop".
on shutdown and compiled as a windows, not console, app, the app never
exists the "event loop" but rather just gets killed. This gives
my code no
opportunity to release resources it is holding on to etc. I have
all the py2exe email archives and the site but nothing seems to address how
the exe gets "shutdown".
I am looking for a way for me to run some code when Windows signals a
shutdown (or logoff). BTW I realize this is a possible for a service but
want to keep my app as an app for now.