Posted by: Cirilo Meggiolaro | 12/11/2008

Tip of the day #58 – Listening to system events from your .NET application

Did you know that your .NET application can handle system events and perform tasks based on system information?

Let me put an example to make it clear: Imagine you have an application running in laptops and you want to tell the user that the laptop battery level is low, advice the user that he needs to save and so on.

Yes, your application can.

Under the Microsoft.Win32 namespace you have the SystemEvents type that allows you to handle some events raised from the operation system.

The following system events may be handled by your application:

  • DisplaySettingsChanged: The event occurs when the display settings have changed;
  • DisplaySettingsChanging: The event occurs when the display settings are changing;
  • EventsThreadShutdown: The event occurs when the thread that listen to the system events is terminated;
  • InstalledFontsChanged: The event occurs when fonts are added to or removed from the system;
  • PaletteChanged: The event occurs when an application that uses a different palette is switched with your current application;
  • PowerModeChanged: The event occurs when the status of the power is changed or the computer hibernates or resumes and so on;
  • SessionEnded: The event occurs when the user shuts down or logs off the system;
  • SessionEnding: The event occurs when the user is requesting the system to shut down or log off;
  • SessionSwitch: The event occurs when the user is switched;
  • TimeChanged: The event occurs when the time from the Windows clock is changed;
  • TimerElapsed: The event occurs when the Windows timer interval has expired;
  • UserPreferenceChanged: The event occurs when the user preferences have changed;
  • UserPreferenceChanging: The event occurs when the user preferences are changing.

How to…

To allow your application to listen for system events you must add an event handler delegate and the method that the delegate is pointing to. The following code snippet defines an event handler delegate and the method that will be invoked when the power status changes:

SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);

void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) {}

Some of the event arguments contain specific properties that give you more information about the event. The PowerModeChangedEventArgs provides a PowerModes enumerator property that lists the options Resume, StatusChange and Suspend.

Tomorrow we are going to check how to retrieve information from the system.

Enjoy it!


Responses

  1. This site is becoming a real container of .NET day by day tasks.


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

Categories

%d bloggers like this: