Sample code for Power Notifications

P

Pinkesh Jain

Hi,

I need to trap the Power related events (On/Off/Suspend
etc) of a handheld device in my application. Can someone
please point me to a sample implementation of the same.
I'm using VB.NET (VS.NET 2003), and the handheld device
has WIndows Ce.Net 4.01 Build 908.

Thanks.
 
C

Chris Tacke, eMVP

Applications are not notified of power state changes, only drivers are. You
could write a driver that would then send out notifications.
 
P

Pinkesh Jain

Hi Chris,

Thanks for the prompt reply.
My requirement is that if the handheld is left idle and
then powered up after sometime, then I want to perform
some action in my application. Could you please suggest
how should I go about it. I went thru the Power Management
reference present in the Windows CE Sdk. But I'm not able
to call the RequestPowerNotifications() and other related
methods. Could you please point me to some place where I
can find a reference implementation for the same.
Thanks.
 
A

Alex Feinman [MVP]

That I believe is achieved by using CeRunAppAtEvent with
NOTIFICATION_EVENT_WAKEUP
 
P

Pinkesh Jain

Hi Alex,

The function "CeRunAppAtEvent" will run the application
specified when the corresponding event gets fired.

But my requirement is that if the application is active
when the Handheld wakes up from Idle mode, then I need to
perform some action withing my application.
How can I achieve this using the "CeRunAppAtEvent"
function?

Thanks.
 
P

Paul G. Tobey [eMVP]

Run a secondary application and have it notify, in some agreed-upon way, the
running copy of your main application.

Paul T.
 
G

Guest

Hi Paul,

Is there anyway I can handle the notification event in the
same application, instead of a secondary application?
 
P

Paul G. Tobey [eMVP]

If the application is already running, I don't think that anything happens
when it's registered to run at an event (it may be brought to the front...)

Paul T.
 
P

Pinkesh Jain

What I want, is to execute a certain piece of code only
when the device comes out of Idle mode. In that case I
need to specifically catch the wakeup event, so that I can
execute the code.
To give you an example,
Suppose I want the user to re-authenticate when the device
comes out of Idle mode. In that case I need to show some
authentication screen to the user. But for that I'll need
to somehow catch the wake up event.
Are there any API calls, which can help me achieve this?
 
P

Paul G. Tobey [eMVP]

Here's what I'd do:

Create a thread which is responsible for handling the code that you're
talking about.

Launch the thread on startup of your application.

In the thread, wait on a named event (WaitForSingleObject). When the event
fires, perform the operation you want to perform and go back to waiting for
the event.

Create a simple C/C++ program which simply creates the event (same name as
in your managed code application), and sets it, then exits.

In your managed code application, call CeRunAppAtEvent, passing the name of
the C/C++ application.

Paul T.
 
P

Pinkesh Jain

Hi,

I found the following approach listed in MSDN

"The notification interface to the Power Manager is used
to allow applications and drivers to receive notifications
of power events. To be able to use this feature, the
application must first create a message queue, and then
pass the handle to the message queue to the Power Manager
when it calls the RequestPowerNotifications API. The Power
Manager then delivers the notifications through the
message queue, with each entry formatted as a
POWER_BROADCAST structure."

Which one would be more effective and easier to implement?
The one listed in MSDN, or the approach suggested by you.






-----Original Message-----
Here's what I'd do:

Create a thread which is responsible for handling the code that you're
talking about.

Launch the thread on startup of your application.

In the thread, wait on a named event
(WaitForSingleObject). When the event
 
P

Paul G. Tobey [eMVP]

I've done the message queue thing. It's a big exercise in P/Invoking (I did
it in C, which is no problem). Note, also, that this may not work on
devices not running Windows CE.NET (PPC 2002, for example), so you should
check on that before diving in head-first.

I could write the two-application scheme in an hour or so. The message
queue scheme would take a day, I'd guess.

Paul T.
 
P

Pinkesh Jain

Hi,

The target for my application will always be Windows
CE.Net. So that's not an issue with me.
Could you please point to the code which shows how to
create and use Message Queues, preferrably in VB.NET.

Thanks.
 
C

Chris Tacke, eMVP

I have the feeling you're charting new territory, so I doubt anyone can
point you to existing VB.NET code on it.
 
P

Paul G. Tobey [eMVP]

I don't have any or know of any, hence my comment about lots of
P/Invoking... I don't see any references to CreateMsgQueue() in the
archives, either...

Paul T.
 
S

Swan B

Excellent sample, thanks.


It does however keep on throwing the exception:
An unhandled exception of type 'System.MissingMethodException' occurred in
PowerAwareApp.exe

This happen every time in the line where you create the message queue:
// Create message queue for power broadcasts
m_hQueue = CreateMsgQueue( "PowerEventWaitQueue", new MSGQUEUEOPTIONS() );

Is it any way this could be device or OS-specific ? (IPAQ 5450/PPC 2002)


Thanks,

Swan B
 
P

Pinkesh Jain

Hi Alex,

Thanks a ton for the sample implementation.
It works great on the Win CE device.
One thing I did note was that, when the device wakes up
from the suspend mode, the form FORM1 is visible, but the
Login screen takes a few seconds to load. Is it something
becuase of the code, or does the device take that much
time?

Thanks for the help.
 
A

Alex Feinman [MVP]

I've noticed the delay as well. Supposedly sending power broadcasts to
applications is not a high-priority thing and get delayed..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top