Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sample code for Power Notifications

41 views
Skip to first unread message

Pinkesh Jain

unread,
Oct 22, 2003, 1:16:02 PM10/22/03
to

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.


Chris Tacke, eMVP

unread,
Oct 22, 2003, 1:35:26 PM10/22/03
to
Applications are not notified of power state changes, only drivers are. You
could write a driver that would then send out notifications.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message
news:02ca01c398c0$2b133e10$a101...@phx.gbl...

Pinkesh Jain

unread,
Oct 22, 2003, 1:52:10 PM10/22/03
to
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.

>.
>

Alex Feinman [MVP]

unread,
Oct 22, 2003, 2:04:10 PM10/22/03
to
That I believe is achieved by using CeRunAppAtEvent with
NOTIFICATION_EVENT_WAKEUP

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:022401c398c5$377bb7e0$a301...@phx.gbl...

Pinkesh Jain

unread,
Oct 22, 2003, 2:32:40 PM10/22/03
to

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.

>.
>

Paul G. Tobey [eMVP]

unread,
Oct 22, 2003, 2:36:50 PM10/22/03
to
Run a secondary application and have it notify, in some agreed-upon way, the
running copy of your main application.

Paul T.

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:03cc01c398ca$dfdec440$a101...@phx.gbl...

anon...@discussions.microsoft.com

unread,
Oct 22, 2003, 2:44:33 PM10/22/03
to

Hi Paul,

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

>.
>

Paul G. Tobey [eMVP]

unread,
Oct 22, 2003, 2:58:47 PM10/22/03
to
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.

<anon...@discussions.microsoft.com> wrote in message
news:0a5501c398cc$88c63fb0$a001...@phx.gbl...

Pinkesh Jain

unread,
Oct 22, 2003, 3:27:13 PM10/22/03
to

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?

>.
>

Paul G. Tobey [eMVP]

unread,
Oct 22, 2003, 3:33:36 PM10/22/03
to
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.

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:0acb01c398d2$7f1a25c0$a001...@phx.gbl...

Pinkesh Jain

unread,
Oct 22, 2003, 4:11:35 PM10/22/03
to
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.

>.
>

Paul G. Tobey [eMVP]

unread,
Oct 22, 2003, 4:22:23 PM10/22/03
to
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.

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:03b901c398d8$b14fc9e0$a301...@phx.gbl...

Pinkesh Jain

unread,
Oct 22, 2003, 4:35:54 PM10/22/03
to
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.


Chris Tacke, eMVP

unread,
Oct 22, 2003, 4:44:12 PM10/22/03
to
I have the feeling you're charting new territory, so I doubt anyone can
point you to existing VB.NET code on it.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:040401c398dc$1765f210$a301...@phx.gbl...

Paul G. Tobey [eMVP]

unread,
Oct 22, 2003, 4:44:28 PM10/22/03
to
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.

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:040401c398dc$1765f210$a301...@phx.gbl...

Alex Feinman [MVP]

unread,
Oct 23, 2003, 3:00:59 AM10/23/03
to
I've put together a sample using this API:
http://www.alexfeinman.com/download.asp?doc=PowerAwareApp.zip


"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:03b901c398d8$b14fc9e0$a301...@phx.gbl...

Swan B

unread,
Oct 23, 2003, 5:28:34 AM10/23/03
to
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

"Alex Feinman [MVP]" <publi...@alexfeinman.com> wrote in message
news:%23WdWrNT...@tk2msftngp13.phx.gbl...

Pinkesh Jain

unread,
Oct 23, 2003, 7:35:35 AM10/23/03
to
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.

>.
>

Chris Tacke, eMVP

unread,
Oct 23, 2003, 10:42:55 AM10/23/03
to
Yes, It's a CE.NET API, so will only be availble on a PPC 2003.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net

"Swan B" <nos...@nospam.com> wrote in message
news:3f97a0b0$1...@news.broadpark.no...

Alex Feinman [MVP]

unread,
Oct 23, 2003, 12:23:26 PM10/23/03
to
I've noticed the delay as well. Supposedly sending power broadcasts to
applications is not a high-priority thing and get delayed..

"Pinkesh Jain" <anon...@discussions.microsoft.com> wrote in message

news:042601c39959$c663bc70$a001...@phx.gbl...

Alex Feinman [MVP]

unread,
Oct 23, 2003, 7:35:23 PM10/23/03
to
Thanks for pointing that out. I've updated the sample with a trap for
MissingMethodException and an error message

"Chris Tacke, eMVP" <cta...@spamfree-opennetcf.org> wrote in message
news:%23DZRzPX...@tk2msftngp13.phx.gbl...

0 new messages