[prev in list] [next in list] [prev in thread] [next in thread] 

List:       py2exe-users
Subject:    Re: [Py2exe-users] Executing code at shutdown (windows)
From:       "Mark Hammond" <mhammond () skippinet ! com ! au>
Date:       2008-08-03 22:49:48
Message-ID: 04c501c8f5bb$3ec35fa0$bc4a1ee0$ () com ! au
[Download RAW message or body]

This is a multipart message in MIME format.

[Attachment #2 (multipart/alternative)]
This is a multipart message in MIME format.


The next thing I'd try is creating a hidden top-level window and seeing if
that gets closed - you'd probably need to create your own thread for the
window and the message loop.  Some of the win32\Demos\win32gui_*.py have
hidden windows you may be able to adapt.

 

Cheers,

 

Mark

 

From: Mats Myrberg [mailto:mats.myrberg@gmail.com] 
Sent: Monday, 4 August 2008 5:54 AM
To: Mark Hammond
Cc: py2exe-users@lists.sourceforge.net
Subject: Re: [Py2exe-users] Executing code at shutdown (windows)

 

I did try this and while reading the API docs it seems SetConsoleCtrlHandler
only applies to console apps.  My app is a windows app (vs console app) and
so SetConsoleCtrlHandler does not seem to work for my app.  And that seems
to be the case when I run my app as well.

 

It seems like a great solution for console apps.  Are there parallel
solutions for a "faceless" windows apps?  I realize I could create a service
but I am trying to stay away from that for various reasons.

 

Mats

On Fri, Aug 1, 2008 at 7:31 PM, Mark Hammond <mhammond@skippinet.com.au>
wrote:

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
def console_ctrl_handler(evt):
   # NOTE that this is called on its own thread
...
   # False means "run the next/default handler", True means don't call
others.
   rc = False
   # During a log-off etc process the console is also closed, so this works
   # OK here.
   if evt in (win32con.CTRL_CLOSE_EVENT, win32con.CTRL_LOGOFF_EVENT):
... do something to *asynchronously* close the app.
       rc = True
   return rc

...
win32api.SetConsoleCtrlHandler(console_ctrl_handler, 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)  Also,
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.

HTH,

Mark
-----------
From: py2exe-users-bounces@lists.sourceforge.net
[mailto:py2exe-users-bounces@lists.sourceforge.net] On Behalf Of Mats
Myrberg
Sent: Friday, 1 August 2008 7:08 PM
To: py2exe-users@lists.sourceforge.net
Subject: [Py2exe-users] Executing code at shutdown (windows)


Hi,

I have a windows app that behaves strangely on shutdown.  Basically, the app
is using twisted and uses twisted's reactor as the "event loop".  However,
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 looked at
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.

TIA,

Mats



 


[Attachment #5 (text/html)]

<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-reply;
	font-family:"Calibri","sans-serif";
	color:#1F497D;}
.MsoChpDefault
	{mso-style-type:export-only;}
@page Section1
	{size:612.0pt 792.0pt;
	margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
	{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>The next thing I'd try is creating a hidden top-level window and
seeing if that gets closed - you'd probably need to create your own thread for
the window and the message loop.&nbsp; Some of the win32\Demos\win32gui_*.py have
hidden windows you may be able to adapt.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>&nbsp;<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Cheers,<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Mark<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'>

<div>

<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>

<p class=MsoNormal><b><span \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Mats Myrberg \
[mailto:mats.myrberg@gmail.com] <br> <b>Sent:</b> Monday, 4 August 2008 5:54 AM<br>
<b>To:</b> Mark Hammond<br>
<b>Cc:</b> py2exe-users@lists.sourceforge.net<br>
<b>Subject:</b> Re: [Py2exe-users] Executing code at shutdown \
(windows)<o:p></o:p></span></p>

</div>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<div>

<p class=MsoNormal>I did try this and while reading the API docs it
seems&nbsp;SetConsoleCtrlHandler only applies to console apps. &nbsp;My app is
a windows app (vs console app) and so&nbsp;SetConsoleCtrlHandler does not seem
to work for my app. &nbsp;And that seems to be the case when I run my app as
well.<o:p></o:p></p>

<div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

<div>

<p class=MsoNormal>It seems like a great solution for console apps. &nbsp;Are
there parallel solutions for a &quot;faceless&quot; windows apps? &nbsp;I
realize I could create a service but I am trying to stay away from that for
various reasons.<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

<div>

<p class=MsoNormal style='margin-bottom:12.0pt'>Mats<o:p></o:p></p>

<div>

<p class=MsoNormal>On Fri, Aug 1, 2008 at 7:31 PM, Mark Hammond &lt;<a
href="mailto:mhammond@skippinet.com.au">mhammond@skippinet.com.au</a>&gt;
wrote:<o:p></o:p></p>

<p class=MsoNormal>You will probably find this isn't specific to py2exe and it
behaves the same<br>
when running under pythonw.exe - if that isn't true, you should probably<br>
chase up why things are different rather than re-inventing your own. &nbsp;But<br>
something like the following might help:<br>
<br>
import win32api, win32con<br>
def console_ctrl_handler(evt):<br>
&nbsp; &nbsp;# NOTE that this is called on its own thread<br>
...<br>
&nbsp; &nbsp;# False means &quot;run the next/default handler&quot;, True means
don't call<br>
others.<br>
&nbsp; &nbsp;rc = False<br>
&nbsp; &nbsp;# During a log-off etc process the console is also closed, so this
works<br>
&nbsp; &nbsp;# OK here.<br>
&nbsp; &nbsp;if evt in (win32con.CTRL_CLOSE_EVENT, win32con.CTRL_LOGOFF_EVENT):<br>
... do something to *asynchronously* close the app.<br>
&nbsp; &nbsp; &nbsp; &nbsp;rc = True<br>
&nbsp; &nbsp;return rc<br>
<br>
...<br>
win32api.SetConsoleCtrlHandler(console_ctrl_handler, True)<br>
<br>
Note that the above is untested and was pulled from an app that will either<br>
have a console or a real window, so just uses win32con.CTRL_CLOSE_EVENT (and<br>
relies on the window being closed at logoff) so you might need to experiment<br>
with exactly which events you need to handle (and as I implied above, try<br>
and find out if twisted is expected to handle this situation ok already, or<br>
already has a &quot;Console Control Handler&quot; that needs tweaking, etc)
&nbsp;Also,<br>
take care of threading issues - unlike a unix signal, this comes on a new<br>
thread created just for the purpose of delivering the event, so you will<br>
need to find out how to break your event loop externally.<br>
<br>
HTH,<br>
<br>
Mark<br>
-----------<br>
From: <a href="mailto:py2exe-users-bounces@lists.sourceforge.net">py2exe-users-bounces@lists.sourceforge.net</a><br>
 [mailto:<a href="mailto:py2exe-users-bounces@lists.sourceforge.net">py2exe-users-bounces@lists.sourceforge.net</a>]
 On Behalf Of Mats<br>
Myrberg<br>
Sent: Friday, 1 August 2008 7:08 PM<br>
To: <a href="mailto:py2exe-users@lists.sourceforge.net">py2exe-users@lists.sourceforge.net</a><br>
                
Subject: [Py2exe-users] Executing code at shutdown (windows)<o:p></o:p></p>

<div>

<div>

<p class=MsoNormal style='margin-bottom:12.0pt'><br>
Hi,<br>
<br>
I have a windows app that behaves strangely on shutdown. &nbsp;Basically, the
app<br>
is using twisted and uses twisted's reactor as the &quot;event loop&quot;.
&nbsp;However,<br>
on shutdown and compiled as a windows, not console, app, the app never<br>
exists the &quot;event loop&quot; but rather just gets killed. &nbsp;This gives
my code no<br>
opportunity to release resources it is holding &nbsp;on to etc. &nbsp;I have
looked at<br>
all the py2exe email archives and the site but nothing seems to address how<br>
the exe gets &quot;shutdown&quot;. &nbsp;<br>
<br>
I am looking for a way for me to run some code when Windows signals a<br>
shutdown (or logoff). &nbsp;BTW I realize this is a possible for a service but<br>
want to keep my app as an app for now.<br>
<br>
TIA,<br>
<br>
Mats<br>
<br>
<o:p></o:p></p>

</div>

</div>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

</div>

</div>

</div>

</body>

</html>



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

_______________________________________________
Py2exe-users mailing list
Py2exe-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/py2exe-users


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic