[python-win32] Catch system level events in python
Jan Schneider
jan at janschneider.net
Wed Mar 3 19:47:50 CET 2010
Am Montag, 1. März 2010 03:16:41 schrieb Tim Roberts:
> You wrote:
> >i am developing a windows service using python-win32.
> >Now I am searching for a possibility to get a notification when a user is
> >trying to shutdown the system.
> >In certain cases I need to cancel this shutdown,
> >Does anyone knows how this could be done?
>
> This is trickier than you might think. Many shutdowns cannot be blocked.
> For example, if battery power is critically low, you're going down, like
> it or not. Further, if someone has their laptop set to hibernate when the
> close the lid, they're going to be really pissed off if your application
> blocks that and the battery expires.
>
> On Vista and beyond, you can use ShutdownBlockReasonCreate to say "the
> system should not be shutdown for a while".
>
> >Is it possible to catch system level events in python like in other
>
> >programming languages as for example C#:
> Anything that can be done in C# can be done in Python. Do you have a C#
> example you want to copy?
>
> >http://mail.python.org/pipermail/python-win32/2008-January/006645.html
>
> Is this code not sufficient for your needs?
Thanks for your answer!
The code is good to get a notification when the system shuts down.
But I can not see a possibility to cancel the shutdown.
I think what i need is something like the code below, but I don't know how to
implement this in python:
http://blogs.techrepublic.com.com/howdoi/?p=135
SystemEvents.SessionEnding += new
SessionEndingEventHandler(SystemEvents_SessionEnding);
void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
e.Cancel();
}
The e.Cancel() should cancel the shutdown.
My next problem is to tell the difference between a shutdown (poweroff) and a
reboot.
What I want to do is:
* catch a shutdown
* decide if it is a power off or reboot
* do some tasks
* shut down or restart the machine
More information about the python-win32
mailing list