Using System events under. NET to enhance your application

Source: Internet
Author: User
Tags documentation log thread microsoft c
In general, when developing applications, it is rarely considered that some changes in the system will affect the application, such as the increase or decrease of fonts in the System font library, user logoff or system shutdown, desktop theme transformations, and so on. For the application of different, more or less will have some impact, such as the program is not exited during the operation of the user log off, may cause data loss, system font changes or system clock changes to the program caused by the interface display or processing impact. If you need a program that is robust, you need to do something about these events as they occur. Luckily for us, we don't need to write a bunch of code on our own,. NET FCL provides a class Microsoft.Win32.SystemEvents to perform this task well, looking at the master Charles Petzold's Microsoft C # Windows Program Design when found this class, presumably many people have been used, but still decided to write some code to experience.
Open the. NET Framework SDK documentation to view systemevents information, which provides some static events as a class, and I tested some of the events by creating a new Windows project.
Registers a bunch of static events in the form constructor.
Public Form1 ()
{
InitializeComponent ();
Occurs when the user changes the display settings.
Systemevents.displaysettingschanged + = new EventHandler (systemevents_displaysettingschanged);
Occurs before the thread that listens for system events terminates. The delegate is invoked on the event thread.
Systemevents.eventsthreadshutdown + = new EventHandler (Systemevents_eventsthreadshutdown);
Occurs when a font is added or removed from the system by the user.
Systemevents.installedfontschanged + = new EventHandler (systemevents_installedfontschanged);
Occurs when the system has run out of available RAM.
Systemevents.lowmemory + = new EventHandler (systemevents_lowmemory);
Occurs when the user switches to an application that uses a different palette.
Systemevents.palettechanged + = new EventHandler (systemevents_palettechanged);
Occurs when the user hangs or continues the system.
Systemevents.powermodechanged + = new Powermodechangedeventhandler (systemevents_powermodechanged);
Occurs when the user logs off or shuts down the system.
systemevents.sessionended + = new Sessionendedeventhandler (systemevents_sessionended);
Occurs when a user attempts to log off or shut down the system.
Systemevents.sessionending + = new Sessionendingeventhandler (systemevents_sessionending);
Occurs when the user changes the time on the system clock.
Systemevents.timechanged + = new EventHandler (systemevents_timechanged);
Occurs when the window timer interval expires.
systemevents.timerelapsed + = new Timerelapsedeventhandler (systemevents_timerelapsed);
Occurs after a user preference item has been changed.
Systemevents.userpreferencechanged + = new Userpreferencechangedeventhandler (systemevents_userpreferencechanged);
Occurs when a user preference item is changed.
Systemevents.userpreferencechanging + = new Userpreferencechangingeventhandler (systemevents_userpreferencechanging );
}
The code above is clear and simple, and the tests for several of these event handlers are as follows.
private void Systemevents_userpreferencechanging (object sender, Userpreferencechangingeventargs e)
{
Userpreferencecategory category = E.category;
MessageBox.Show (category. ToString ());
}
Userpreferencecategory is an enumeration type, with each value representing the user preference area that identifies the changed, with more types, specific to the MSDN documentation, similar to userpreferencechanged events, Use the Userpreferencechangedeventargs parameter.
The following is the system logout or Shutdown event handler.
private void Systemevents_sessionending (object sender, Sessionendingeventargs e)
{
If MessageBox.Show (this, "allows the system to log off!) "," System hint ", Messageboxbuttons.yesno)!= Dialogresult.yes)
{
E.cancel = true;
}
Else
{
E.cancel = false;
}
sessionendreasons reason = E.reason;
switch (reason)
{
Case Sessionendreasons.logoff:
MessageBox.Show ("The user is logging off.) The operating system continues to run, but the user who started the application is logging off. ");
Break
Case Sessionendreasons.systemshutdown:
MessageBox.Show (the operating system is shutting down.) ");
Break
}
}
If you change the above event handler to the following
private void Systemevents_sessionending (object sender, Sessionendingeventargs e)
{
E.cancel = true;
}
What will happen, you click the Start menu Shutdown Select Logoff, shutdown, or restart will be invalidated, the computer can not shut down normally, further words to the program into Windows services, people do not know, dizzy, mischief?
sessionended events, the event argument class is Sessionendedeventargs, and the Cancel property is less than Sessionendingeventargs, and the Cancel property is similar to some events under Windows. such as form.closing events, control.validating events.
Added, if you need to get the system information the application needs, you can access the System.Windows.Forms.SystemInformation class, which is also a useful class that provides a set of static properties.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.