1 out of 1 rated this helpful - Rate this topic

SystemEvents.PowerModeChanged Event

Occurs when the user suspends or resumes the system.

Namespace: Microsoft.Win32
Assembly: System (in system.dll)

public static event PowerModeChangedEventHandler PowerModeChanged
/** @event */
public static void add_PowerModeChanged (PowerModeChangedEventHandler value)

/** @event */
public static void remove_PowerModeChanged (PowerModeChangedEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.
Exception type Condition

InvalidOperationException

System event notifications are not supported under the current context. Server processes, for example, might not support global system event notifications.

ExternalException

The attempt to create a system events window thread did not succeed.

Caution noteCaution:

Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Must detach from event (C++ version)

In order to prevent a memory leak, as noted, you must unregister from this event. To detach from the event in C++, keep a reference to the handler-delegate until disposal time, something like this:

ref class Test
{
    // Here's the reference we'll keep until we've unregistered
    Microsoft::Win32::PowerModeChangedEventHandler^ m_powerModeChangedDelegate;

 







public:
    Test()
    {

 

        // Create the delegate (for pedants, note that we don't use an initializer

 

        // list since we are making explicit use of the "this" pointer).

        m_powerModeChangedDelegate =
              gcnew Microsoft::Win32::PowerModeChangedEventHandler( 
                  this, &Test::PowerModeChangedHandler );

 





        // Register for host power mode changes
        Microsoft::Win32::SystemEvents::PowerModeChanged += m_powerModeChangedDelegate;
    }

 




    ~Test() // Destructor is the "Dispose" method in C++/CLI
    {
        // Unregister for host power mode changes
        Microsoft::Win32::SystemEvents::PowerModeChanged -= m_powerModeChangedDelegate;
    }

 


    void PowerModeChangedHandler( 
        Object^ sender, Microsoft::Win32::PowerModeChangedEventArgs^ e )
    {
        // ... handle event here ...
    }
};
Must detach from event

As cautioned above, if attaching to this event to an instance method, as opposed to a static (Shared in Visual Basic) method, you must detach from the event or the attached object will be around in memory until the process exits. This is typically done in the dispose method of the object.

The following example shows a form that attaches to the SystemEvents.PowerModeChanged event and then detaches from the event in its Dispose(bool) implementation.

[C#]
  
using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Samples
{
public class MyForm : Form
{
public MyForm()
{
SystemEvents.PowerModeChanged += OnPowerModeChanged;
}

        protected override void Dispose(bool disposing)
{
if (disposing)
{
SystemEvents.PowerModeChanged -= OnPowerModeChanged;
}

            base.Dispose(disposing);
}

        private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
MessageBox.Show("PowerModeChanged");
}
}
}
Event is raised on the UI thread
Starting in .NET 2.0, this event is now raised on the Main UI thread (or more correctly via AsyncOperationManager.SynchronizationContext, which by default is the UI thread in Windows Forms). This means that you can safely touch members on UI controls that were created on that same thread.