[Tutor] How can I catch a Windows event?

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Sep 2 23:44:32 CEST 2004


> I am new in computing and python. My problem is during
> switching off/ restaring Win 98 the phyton script written by me
> doesn’t stop. (in Win XP. no problem)

Caveat: The following are personal observations which are
not based on deep knowledge but my own painful experience.

OK, How are you running the program? Is it starting as a service
or as a regular program from the start menu or Startup group?
Also how are you switching Windows off? Windows 98 is not very
well behaved at the best of times and doesn't like threads much
at all, barely tolerates them some would say! So unless your main
program keeps track of all the threads you start and closes
them down properly you will have problems, especially if somebody
just hits the off switch..

> I guess it is because of a thread. By checking from the internet
> I found the WM_QUERYENDSESSION should be caught for regular
> stopping, but I do not understand how.

Are you running a GUI or console application? Console apps don't
get windows events like Windows apps do under '98 - they do in
XP since the console is really a windows app, but in '9x they
are glorified DOS boxes so you need to pick up the DOS interrupt
handler. Unfortunately this is difficult from normal code!

> I am also not sure, which I should use win 32all or wxPython.

Win32all is the package if you want to mess with low level
windows events. OTOH wxPython should trap the message for you
and close down cleanly.

> The following script stops the win98 shutdown process
> if I start the script from consol.

That should be because Windows sees the console running a
background process and knows it can't stop it so it just
kind of gives up and asks you to kill it. This regularly
happens to me if I run Cygwin with background jobs!

It's what's supposed to happen and is "A Good Thing" IMHO.
The alternative would be fore Windows to just kill the
application outright when it migt be in the middle of
a major file operation and thus destroy reams of data...

If you want better behaviour you need to turn your app into
a proper GUI and catch the events from Windows.

I'mn not sure if that clarifies or confuses further!

Alan G.



More information about the Tutor mailing list