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.