Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server