C # capture windows Shutdown events and do what you want to do before the system is shut down,

Source: Internet
Author: User

C # capture windows Shutdown events and do what you want to do before the system is shut down,

C # capture windows Shutdown events and do what you want before the system is shut down;

Sometimes we may want to record or handle some things during Windows shutdown. Here are several methods.

Method 1:

/// <Summary> /// callback function of the window process /// </summary> /// <param name = "m"> </param> protected override void WndProc (ref Message m) {switch (m. msg) {// This message is in case WindowsMessage before OnFormClosing. WM_QUERYENDSESSION: // MessageBox. show ("WndProc. WM_QUERYENDSESSION. I want to prevent the system from being shut down! "); // This. close (); // this. dispose (); // Application. exit (); m. result = (IntPtr) 1; // Prevents Windows from logging out, shutting down, or restarting break; default: break;} base. wndProc (ref m );}
Method 2:
Protected override void OnFormClosing (FormClosingEventArgs e) {switch (e. CloseReason) {case CloseReason. ApplicationExitCall: e. Cancel = true; MessageBox. Show ("intercept the request event! "); Break; case CloseReason. FormOwnerClosing: e. Cancel = true; MessageBox. Show (" intercept the event itself! "); Break; case CloseReason. MdiFormClosing: e. Cancel = true; MessageBox. Show (" block the MDI form close event! "); Break; case CloseReason. None: break; case CloseReason. TaskManagerClosing: e. Cancel = true; MessageBox. Show (" intercept the task manager to close the event! "); Break; case CloseReason. UserClosing: // this event will be triggered when you log out or shut down; // MessageBox. Show (" intercept user close events! "); E. Cancel = false; break; case CloseReason. WindowsShutDown: e. Cancel = true; MessageBox. Show (" Block shutdown events! "); Break; default: break;} base. OnFormClosing (e );}
Method 3:

// Occurs when the user attempts to log out or shut down the system. SystemEvents. sessionEnding + = new SessionEndingEventHandler (SystemEvents_SessionEnding); // The system deregister or closes the event handler, private void SystemEvents_SessionEnding (object sender, SessionEndingEventArgs e) {if (MessageBox. show (this, "whether to allow system logout! "," System prompt ", 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 out. The operating system continues to run, but the user who started the application is logging out. "); 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 can click the Start menu to shut down and choose to log off, shut down, or restart. The computer cannot be shut down normally, what's more, make the program into a Windows Service, dizzy, and prank? // The SessionEnded event is the same as the preceding one. The event parameter class is SessionEndedEventArgs. The Cancel attribute is less than SessionEndingEventArgs, And the Cancel attribute is similar to some windows events, such as Form. closing event, Control. validating event. // Supplement: If you need to obtain the System information required by the application, you can access the System. windows. forms. systemInformation class, which is also a useful class, provides a set of static attributes.
But it was not executed here during debugging!


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.