C # captures Windows shutdown events and does something you want to do before the system shuts down

Source: Internet
Author: User

C # captures Windows shutdown events and does what it wants to do before the system shuts down;

Sometimes we might want to record or process something when Windows shuts down, and here are a few ways to do it.

Method One:

        <summary>///        window procedure callback function///</summary>//        <param name= "M" ></param>        protected override void WndProc (ref message m)        {            switch (m.msg)            {                //This message before onformclosing                case Windowsmessage.wm_queryendsession:                    //messagebox.show ("wndproc.wm_queryendsession. I want to stop the system from shutting down!" ");                    This. Close ();                    This. Dispose ();                    Application.exit ();                    M.result = (IntPtr) 1; Prevent Windows from logging off, shutting down, or restarting the break                    ;                Default: Break                    ;            }            Base. WndProc (ref m);        }
Method Two:
        protected override void Onformclosing (FormClosingEventArgs e) {switch (E.closereason)                    {Case Closereason.applicationexitcall:e.cancel = true; MessageBox.Show ("Intercept shutdown request event!                    ");                Break                    Case closereason.formownerclosing:e.cancel = true; MessageBox.Show ("Intercept your own Shutdown event!")                    ");                Break                    Case closereason.mdiformclosing:e.cancel = true; MessageBox.Show ("Block MDI form Shutdown event!                    ");                Break                Case CloseReason.None:break;                    Case closereason.taskmanagerclosing:e.cancel = true; MessageBox.Show ("Intercept Task Manager Shutdown event!")                    ");                Break Case closereason.userclosing://Log off or off the opportunity to trigger this event;//messagebox.show (" Block User shutdown Events!        ");            E.cancel = false;                Break                    Case closereason.windowsshutdown:e.cancel = true; MessageBox.Show ("Intercept Shutdown event!                    ");                Break            Default:break; } base.        Onformclosing (e); }
Method Three:

Occurs when the user tries to log off or shut down the system. Systemevents.sessionending + = new Sessionendingeventhandler (systemevents_sessionending);//The following is the system logoff or Shutdown event handler, Priva te void Systemevents_sessionending (object sender, Sessionendingeventargs e) {if (MessageBox.Show (this, "Allow 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 the above event handler is modified to the following//private void Systemevents_sessionending (object sender, Sessionendingevent   Args e)//{       E.cancel = true;         }//That will happen, you click the Start menu Shutdown Select Logout, shutdown, or restart will be invalid, the computer can not shut down properly, further the program into Windows services, dizzy, prank? Sessionended event, 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 the Form.Closing event, the Control.validating event. Add that if you need to get the system information your application needs, you can access the System.Windows.Forms.SystemInformation class, which is also a useful class that provides a set of static properties.
But when debugging did not execute here!

C # captures Windows shutdown events and does something you want to do before the system shuts down

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.