tags:

views:

637

answers:

6

OK that does it. For the umpteenth time my laptop just shut down in the middle of my game because my power cable had disconnected without me noticing it.

Now I want to write a little C# program that detects when my power cable disconnects and then emits a nice long System beep. What API could I use for that?

+2  A: 

This will probably get closed as not-programming-related, but you may want to check your BIOS setttings.

My Lenovo laptop does exactly what you're suggesting natively. There is a setting in my BIOS for beeping on power status change.

BQ
Notice how he cleverly worded it as a programming question. I can imagine other circumstances where a program would want to know if it was running on battery power, so it's a good question to have in the database.
Mark Ransom
A: 

This may very much depend on your exact operating system. Here are some calls for windows XP, I am sure you can find the Vista equivalents:

http://msdn.microsoft.com/en-us/library/ms704147(VS.85).aspx

Uri
A: 

Can't think of anything directly accessible via the .NET framework, but I do know that Intel has the Mobile Platform SDK with .NET libraries that should provide this information to you. It's possible AMD has an equivilent somewhere.

Intel Mobile Platform SDK

invenetix
+9  A: 

This should be trivial to implement using the SystemInformation.PowerStatus property. And even though that lives in Windows.Forms, it should be perfectly usable from a system service.

For a solution that also works on the Compact Framework, see HOWTO: Get the Device Power Status

mdb
+4  A: 

SystemEvents.PowerModeChanged. You will need to use either GetSystemPowerStatus (see link in one of the answers) or SystemInformation.PowerStatus (link is in another answer) in the handler to check what happened.

liggett78
+2  A: 

To continue BQ's answer, there are power settings in Windows that can be changed when the power cord is removed. Since I sometimes work with it removed I didn't change the power settings on my machine, but when the battery approaches 15% the screen brightness is set to the lowest level, making it possible to work (and easily changeable with Fn-Home key) but very noticeable so that I plug the cable back in time.

configurator