SystemEvents Class
Assembly: System (in system.dll)
The SystemEvents class provides the ability to respond to specific types of system events.
When a system event is raised, any delegates attached to the event are called using the thread that monitors for system events. Therefore, you should make any calls from your event handlers thread-safe. If you need to call a system event that is not exposed as a member of this class, you can use the InvokeOnEventsThread method.
Caution: |
|---|
| Do not perform time-consuming processing on the thread that raises a system event handler because it might prevent other applications from functioning. |
The following code example registers interest in some system events and then waits for any of those events to occur. The output shown occurs if the user changes the display resolution.
using System; using Microsoft.Win32; public sealed class App { static void Main() { // Set the SystemEvents class to receive event notification when a user // preference changes, the palette changes, or when display settings change. SystemEvents.UserPreferenceChanging += new UserPreferenceChangingEventHandler(SystemEvents_UserPreferenceChanging); SystemEvents.PaletteChanged += new EventHandler(SystemEvents_PaletteChanged); SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); // For demonstration purposes, this application sits idle waiting for events. Console.WriteLine("This application is waiting for system events."); Console.WriteLine("Press <Enter> to terminate this application."); Console.ReadLine(); } // This method is called when a user preference changes. static void SystemEvents_UserPreferenceChanging(object sender, UserPreferenceChangingEventArgs e) { Console.WriteLine("The user preference is changing. Category={0}", e.Category); } // This method is called when the palette changes. static void SystemEvents_PaletteChanged(object sender, EventArgs e) { Console.WriteLine("The palette changed."); } // This method is called when the display settings change. static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { Console.WriteLine("The display settings changed."); } } // This code produces the following output. // // This app is waiting for system events. // Press <Enter> to terminate this application. // Display Settings changed. // User preference is changing. Category=General
- NamedPermissionSet for full access to system resources. Demand values: LinkDemand. Associated state: FullTrust
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.Despite the Visual Basic example above, and as cautioned on the individual topics for each each event, if attaching an event from this class 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.UserPreferenceChanged event and then detaches in its Dispose(bool) implementation.
[C#]
using System;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Samples
{
public class MyForm : Form
{
public MyForm()
{
SystemEvents.UserPreferenceChanging += OnUserPreferenceChanging;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
SystemEvents.UserPreferenceChanging -= OnUserPreferenceChanging;
}
base.Dispose(disposing);
}
private void OnUserPreferenceChanging(object sender, UserPreferenceChangingEventArgs e)
{
MessageBox.Show("UserPreferenceChanging");
}
}
}
- 8/25/2007
- David M. Kean
- 8/25/2007
- David M. Kean
The above topic incorrect states:
When a system event is raised, any delegates attached to the event are called using the thread that monitors for system events. Therefore, you should make any calls from your event handlers thread-safe.
While this was correct in versions prior to .NET 2.0, these events are 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.
- 8/25/2007
- David M. Kean
- 8/25/2007
- David M. Kean
Caution: