![]() |
![]() |
|
|
|
||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||
|
|
|
|
|
||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
![]() |
|
|
|
|
|
PowerMode, change and status in .NET / System / C# |
|
IntroductionThis articles explains how to make an application aware of the Battery Status when in use on a laptop/tablet. PowerMode change comes from Intel documentation on devx. The current power status comes from MSDN in a longhorn article. BackgroundSee Cancellable Thread Pool for why this is here. This is a very raw and ready post as it's just a couple of searches anyway. The only bit of my code really is the constructor which ensures you have results whenever you instantiate the class. Using the codeRun the application, and then attempt to generate as many With a laptop/tablet, you should be able to generate the full range of events as long as your battery holds some charge. The MSDN/Intel documentation does not indicate if these power events are also available from servers via UPS. If anyone has a development machine on a UPS, I would be interested to see a trace of the events. (The file is automatically saved when the application shuts down, see Code FeaturesThere are two parts to this code, firstly detecting //At the Top of the file
using Microsoft.Win32;
//In the Constructor or elsewhere
SystemEvents.PowerModeChanged
+= new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
Next, it's a simple case of declaring your handler in the usual method. private void SystemEvents_PowerModeChanged(object sender,
PowerModeChangedEventArgs e)
{
The //Get the current Status
PowerStatus ps = new PowerStatus();
switch (e.Mode)
{
case PowerModes.Resume:
if (ps.BatteryFlag & _BatteryStatus.Critical
!= _BatteryStatus.Critical)
{
//Start process that will use lots of CPU
The Points of InterestThis code can be used in combination with Threading to provide applications that will throttle back the CPU usage as power becomes critical. Historyv0.1a 13th Sept 2004. First release. |