Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows 7

Working in the Background : PROVIDING POWER MANAGEMENT (part 2) - Detecting a Change in Monitor State

4/21/2014 1:45:20 AM

3. Detecting a Change in Monitor State

The PowerManager class supports a number of events that help your application react to specific power conditions. For example, the BatteryLifePercentChanged event lets you monitor the battery state and close the application before power runs out. You can also keep track of the power source using the PowerSourceChanged event and suggest the user close your application when the system is running on the battery, in order to conserve battery power for more important tasks.

An extremely useful event is IsMonitorOnChanged. If your application monitors this event, it won't try to interact with the user when the user clearly can't see the output of your application. Listing 2 shows how to use this event.

Example 2. Handling a monitor state change event
public frmMain()
{
// Perform standard initialization.
InitializeComponent();

// Add an event handler for monitor state changes.
PowerManager.IsMonitorOnChanged +=
new EventHandler(PowerManager_IsMonitorOnChanged);
}

void PowerManager_IsMonitorOnChanged(object sender, EventArgs e)
{
// Just show the monitor state.
if (PowerManager.IsMonitorOn)
lstData.Items.Add("The monitor is on!");
else
lstData.Items.Add("The monitor is off!");
}

As with all events, Visual Studio helps you create the code in the frmMain() constructor. As soon as you type +=, the IDE displays a message that tells you to press Tab to create the event handler code. You press Tab a second time to create the event handler itself, PowerManager_IsMonitorOnChanged().

Figure 2. Using events makes it possible to automatically respond to power events.

The example code simply outputs a message in this case. It outputs one message when the monitor is on and another when the monitor is off, as shown in Figure 2. All you need do to see the results of this example is set Windows to turn the monitor off after a minute and then wait for the monitor to turn itself off. The code uses the PowerManager.IsMonitorOn property to determine the current monitor state.

Other -----------------
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 3) - Servicing the Operating System in an Image , Committing an Image
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 2) - Mounting an Image , Servicing Drivers in an Image
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 1) - Viewing Information about an Image with DISM
- Automating Windows 7 Installation : Applying an Image Using ImageX
- Automating Windows 7 Installation : Capturing an Image Using ImageX
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 4) - Fine-tuning Web Pages and Battling Bugs - Saving a Visio Drawing as a Web Page
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 3) - Fine-tuning Web Pages and Battling Bugs - Customizing Web Page Output
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 2) - Exploring Visio-Generated Web Pages
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 1) - Saving as Web Page
- Microsoft Visio 2010 : Sending Visio Files in Email, Saving as PDF or XPS Files
- Microsoft Visio 2010 : Introducing Data Graphics (part 2) - Creating Data Graphics,Applying Data Graphics to Shapes
- Microsoft Visio 2010 : Introducing Data Graphics (part 1) - What Is a Data Graphic?
- Microsoft Visio 2010 : Linking External Data to Shapes (part 6) - Using Link Data - Linking Data to Shapes Using Link Data
- Microsoft Visio 2010 : Linking External Data to Shapes (part 5) - Using Link Data - Preparing a Master for Link Data , Importing Data for Link Data
- Microsoft Visio 2010 : Linking External Data to Shapes (part 4) - Using the Database Wizard - Taking the Data-Linked Light Bulb Shape for a Spin
- Microsoft Visio 2010 : Linking External Data to Shapes (part 3) - Using the Database Wizard - Setting Up the Excel File as a Data Source
- Microsoft Visio 2010 : Linking External Data to Shapes (part 3) - Using the Database Wizard - Setting Up the Excel File as a Data Source
- Microsoft Visio 2010 : Linking External Data to Shapes (part 2) - Preparing the Light Bulb Shape for Data Linking
- Microsoft Visio 2010 : Linking External Data to Shapes (part 1) - Preparing the Data
- Microsoft Visio 2010 : Working with Data - Creating Reports (part 3) - Using Reports with Other Documents
 
 
Most view of day
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 1) = Backing Up App Lists and Settings
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 3) - Backing Up Text Messages
- Windows Phone 7 Programming Model : Asynchronous Programming - Background Threads
- Deploying Applications Using Group Policy and SCCM 2007 : Deploying Applications Using Group Policy
- Windows Server 2003 : Protecting Hosts with Windows Host Firewalls - Firewall Basics
- Windows Server 2008 : Promoting and Demoting a Domain Controller - Demoting a DC with dcpromo, Using dcpromo with an unattend File
- System Center Configuration Manager 2007 : Distributing Packages - Creating Collections (part 1) - Static Collections
- Adobe Dreamweaver CS5 : Using Library Items and Server-side Includes (part 5) - Updating Your Web Sites with Libraries
- Microsoft Content Management Server Development : Validating Placeholder Controls - Validating the SingleImagePlaceholderControl
- Microsoft Visio 2010 : Working with Individual Shapes - Resizing and Rotating Shapes
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro