Making applications environment aware

I hit on this the other day, but started reading more on this tonight. Here are some pretty basic things you can stay abreast of, if you have an application that needs to know about your environment. For example, if you want to react when: a computer is about to suspend, the display properties are changed, network availability changes, etc, etc. Well here is some simple stubbed out code to show how to wire up, then use these system events:

    SystemEvents.DisplaySettingsChanged +=

        new EventHandler(SystemEvents_DisplaySettingsChanged);

 

    SystemEvents.PowerModeChanged +=

        new Microsoft.Win32.PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);

 

    SystemEvents.SessionEnding +=

        new SessionEndingEventHandler(SystemEvents_SessionEnding);

 

    NetworkChange.NetworkAddressChanged +=

        new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

 

    NetworkChange.NetworkAvailabilityChanged +=

        new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

Then, here are the event handlers for these with some sample implementation:

    private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)

    {

        MessageBox.Show("Display settings have changed.");

    }

    private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)

    {

        switch (e.Mode)

        {

            case PowerModes.Resume:

                {

                    MessageBox.Show("…Resuming from a suspended state.");

                    break;

                }

            case PowerModes.StatusChange:

                {

                    MessageBox.Show("Other power event – like battery low or battery charging.");

                    break;

                }

            case PowerModes.Suspend:

                {

                    MessageBox.Show("About to suspend…");

                    break;

                }

        }

    }

    private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)

    {

        if (e.Reason == SessionEndReasons.Logoff)

            MessageBox.Show("User logging off…");

 

        if (e.Reason == SessionEndReasons.SystemShutdown)

            MessageBox.Show("System shutting down…");

 

        // Uncomment to try to cancel the logoff/shutdown

        //e.Cancel = true;

    }

 

 

    private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)

    {

        MessageBox.Show("Network address has changed.");

    }

 

    private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)

    {

        MessageBox.Show("Network is available: " + e.IsAvailable.ToString());

    }

Posted in General, Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: