Catching system shutdown events- on Windows

I've looked through some old posts about this here
http://wxpython-users.1045709.n5.nabble.com/Catching-Shutdown-Events-td2365230.html

and on the wxWiki.

I have tried catching the application shutdown events on my Windows XP
system with Python 2.3.4 and WxPython 2.8.9.1 (msw-ansi)

I can't make it work on my system either, and I'm wondering if anyone
has.

Do I need to get a newer version of wxPython?

I'm actually interested in catching the system shutdown event, so I
can postpone it and backup some files, then continue with the shutdown
after the files have been backed up

Thanks

Hi,

I've looked through some old posts about this here
http://wxpython-users.1045709.n5.nabble.com/Catching-Shutdown-Events-td2365230.html

and on the wxWiki.

I have tried catching the application shutdown events on my Windows XP
system with Python 2.3.4 and WxPython 2.8.9.1 (msw-ansi)

I can't make it work on my system either, and I'm wondering if anyone
has.

Do I need to get a newer version of wxPython?

No, those events unfortunately don't work. You can workaround it using
system libraries to install a termination handler. Included below is a
method from one of my libraries.

This method requires win32api extensions on Windows. You just need
call it with a function as an argument to use as a callback. The
function will get called when the os tries to shutdown the
application.

def InstallTermHandler(callback, *args, **kwargs):
    """Install exit app handler for sigterm (unix/linux)
    and uses SetConsoleCtrlHandler on Windows.
    @param callback: callable(*args, **kwargs)
    @return: bool (installed or not)

    """
    assert callable(callback), "callback must be callable!"

    installed = True
    if wx.Platform == '__WXMSW__':
        if HASWIN32:
            win32api.SetConsoleCtrlHandler(lambda dummy :
callback(*args, **kwargs),
                                           True)
        else:
            installed = False
    else:
        signal.signal(signal.SIGTERM,
                           lambda signum, frame : callback(*args, **kwargs))

    return installed

Works for what I needed it for but YMMV

Cody

···

On Tue, Dec 14, 2010 at 1:00 PM, cappy2112 <cappy2112@gmail.com> wrote:

Thanks- does this mechanism have veto capability?

···

On Dec 14, 11:19 am, Cody Precord <codyprec...@gmail.com> wrote:

Hi,

On Tue, Dec 14, 2010 at 1:00 PM, cappy2112 <cappy2...@gmail.com> wrote:
> I've looked through some old posts about this here
>http://wxpython-users.1045709.n5.nabble.com/Catching-Shutdown-Events-

> and on the wxWiki.

> I have tried catching the application shutdown events on my Windows XP
> system with Python 2.3.4 and WxPython 2.8.9.1 (msw-ansi)

> I can't make it work on my system either, and I'm wondering if anyone
> has.

> Do I need to get a newer version of wxPython?

No, those events unfortunately don't work. You can workaround it using
system libraries to install a termination handler. Included below is a
method from one of my libraries.

This method requires win32api extensions on Windows. You just need
call it with a function as an argument to use as a callback. The
function will get called when the os tries to shutdown the
application.

def InstallTermHandler(callback, *args, **kwargs):
"""Install exit app handler for sigterm (unix/linux)
and uses SetConsoleCtrlHandler on Windows.
@param callback: callable(*args, **kwargs)
@return: bool (installed or not)

&quot;&quot;&quot;
assert callable\(callback\), &quot;callback must be callable\!&quot;

installed = True
if wx\.Platform == &#39;\_\_WXMSW\_\_&#39;:
    if HASWIN32:
        win32api\.SetConsoleCtrlHandler\(lambda dummy :

callback(*args, **kwargs),
True)
else:
installed = False
else:
signal.signal(signal.SIGTERM,
lambda signum, frame : callback(*args, **kwargs))

return installed

Works for what I needed it for but YMMV

Cody

Hi,

···

On Tue, Dec 14, 2010 at 1:30 PM, cappy2112 <cappy2112@gmail.com> wrote:

Thanks- does this mechanism have veto capability?

Depends on what the callback function you pass in does. This just
allows you to be notified that the os is requesting the app to exit.

Cody