Under normal circumstances, rarely consider the changes in the system part when developing applications, such as increased or decreased in the system font library, user logout or system shutdown, desktop topic transformation, etc. For applications, more or less will cause some effects, such as programs that are not exited during operation, it is possible to cause data loss, system font changes, or change changes to the program, or The impact on treatment. If the program is required to have a certain robustness, you need to do some processing when these events occur. Fortunately, we don't need to write some complicated code to implement these features.
.NET FCL
Provided a class
Microsoft.win32.systemevents
Complete this task very well, watching the master
Charles Petzold
"
Microsoft C # windows
When the program is designed, this class is found. If many people have already used it, but they decide to write some code to experience it.
Open the .NET Framework SDK document, check the information of SystemEvents, which provides some static events as a class, and I have a new Windows project testing some events.
Register a bunch of static events in the form constructor,
Public Form1 ()
{
InitializationComponent ();
// occurs when the user changes the display setting.
SystemEvents.DisplaySettingSchanged = New EventHandler (SystemEvents_DisplaySettingSchange);
// happens before the thread of the listening system event occurs. A delegate will be called on the event thread.
SystemEvents.EventSthreadshutdown = New EventHandler (SystemEvents_EventSthreadshutdown);
// occurs when the user adds or removes the font in the system.
SystemEvents.installedFontSchanged = New EventHandler (SystemEvents_InstalledFontSchanged);
// When the system can use RAM to use RAM.
SystemEvents.lowMemory = New EventHandler (SystemEvents_lowmemory);
// occurs when the user switches to the application using other palette.
SystemEvents.palettechanged = New EventHandler (SystemEvents_palettechange);
// occurs when the user hangs or continues the system.
SystemEvents.PowerModeChanged = New PowerModeChangeDeventHandler (SystemEvents_PowerModeChange);
// occurs when the user is logged out or turns off.
SystemEvents.SessionEnded = New sessionEndedEventHandler (SystemEvents_Session Nevant);
// occurs when the user is trying to log out or shut down the system.
SystemEvents.SESSIONDING = New sessionndingEventhandler (SystemEvents_SESSIONENDING);
// occurs when the user changes the time on the system clock.
SystemEvents.timechanged = new eventhandler (systemEvents_timechanged);
// occurs when the window timer is expired.
SystemEvents.TimeRelapsed = new timeelelapsedeventhandler (systemEvents_timeRelapsed); // occurs after the user's preferences change.
Systemevents.UserPreferenceChanged = New UserPreferenceChangeDeventHandler (SystemEvents_UserPreference;
// occurs when the user preferences change.
Systemevents.UserPreferenceChangeing = New UserPreferenceChangingEventHandler (SystemEvents_UserPreference;
}
The above code is clear and simple, and the test of several event handler is as follows,
Private void systemevents_userpreferencechchch (Object Sender, UserPreference E)
{
UserPreference Category = E.CATEGORY;
Messagebox.show (category.toString ());
}
UserPreferenceCategory is an enumeration type, each value indicates that the user's preference area is identified, the type is more, and the user can see the MSDN document, the UserPreferenceChanged event is similar, using the UserPreferenceChangeDeventArgs parameter.
Below is the system logout or closing the event handler,
Private void systemevents_ssssionnding (Object Sender, SessionndingeventArgs E)
{
IF (MSSAGEBOX.SHOW (THIS, "is allowed to log out!", "System Tips", MessageBoxButtons.yesno! = DialogResult.yes)
{
e.cancel = true;
}
Else
{
e.cancel = false;
}
Sessionendreasons Reason = E.reason;
Switch (REASON)
{
Case sessionndreasons.logoff:
Messagebox.show ("users are logging out. The operating system continues to run, but users who launched this application are logging out.");
Break;
Case sessionndreasons.systemshutdown:
Messagebox.show ("The operating system is being closed.");
Break;
}
}
If the above event handler is modified as follows
Private void systemevents_ssssionnding (Object Sender, SessionndingeventArgs E)
{
e.cancel = true;
}
What will happen, you click Start Menu to Shock Select Logout, Shutdown, or Restart will fail. The computer cannot be properly turned off. Further words make the program into a Windows service, people do not know the ghost, halo?
The sessionended event is the same, the event parameter class is SessionEndedEventArgs, and the Cancel property is similar to SessionEndingEventArgs. The Cancel property is similar to some of Windows, such as form.closing events, Control.validating events. Supplement, if you need to get the system information you need, you can access the System.Windows.Forms.SystemInformation class, which is also a very useful class that provides a set of static properties.