The use of c # capture windows shutdown incident

Author:Anonymous    Updated:2008-2-27 22:04:51
Work in the company, signed retire from work when needed, and I do so once Gesanchaiwu will forget. How to do, and can therefore wanted to capture the shutdown windows, a procedure to do it in the shutdown of reminded me about it.

Very lucky very easy to find Microsoft.Win32 namespace below SystemEvents category, he has a static events SessionEnding write-off or shutdown of the system occurred, the incident only in winform effective procedures, and procedures in the console below invalid not excited; Another point in the process we must launch events will be added removed, otherwise it will be easy for a memory overflow.

Key code below:


Using System;
Using System.Collections.Generic;
Using System.Windows.Forms;

Using Microsoft.Win32;

Namespace Shutdown
(
     Static class Program
     (
         /**//// <summary>
         / / / Application's main entrance point.
         / / / </ Summary>
         [STAThread]
         Static void Main ()
         (
             Application.EnableVisualStyles ();
             Application.SetCompatibleTextRenderingDefault (false);
             FormShutdown formShutdown = new FormShutdown ();
             SystemEvents.SessionEnding + = new SessionEndingEventHandler (formShutdown.SystemEvents_SessionEnding);
             Application.Run (formShutdown);
         )

     )
) Form code:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using Microsoft.Win32;

Namespace Shutdown
(
     Public partial class FormShutdown: Form
     (
         Const string MESSAGE_TXT = "sign you retire?";
         Const string MESSAGE_TITLE = "Tip";

         Public FormShutdown ()
         (
             InitializeComponent ();
         )


         Internal void SystemEvents_SessionEnding (object sender, SessionEndingEventArgs e)
         (
             DialogResult result = MessageBox.Show (MESSAGE_TXT, MESSAGE_TITLE, MessageBoxButtons.YesNo);

             E.Cancel = (result == DialogResult.No);
         )

         Private void FormShutdown_Load (object sender, EventArgs e)
         (
             This.Location = new Point (Screen.PrimaryScreen.WorkingArea.Width - 200, 0);
         )

         Protected override void OnClosed (EventArgs e)
         (
             SystemEvents.SessionEnding -= new SessionEndingEventHandler (this.SystemEvents_SessionEnding);
             Base.OnClosed (e);
         )
     )
)
In the use of this procedure in Windows2003 c # 2.0 under test pass. Members SystemEvents.SessionEnding incident in the use of the procedure should bear in mind when removed from the incident.

 

But there are two points of regret:

1. Use of this method can not capture the events dormancy

2. Use of this procedure too much memory, so only a small function of a surprise 12 M memory, it is. Blame goods Net framework; It is inconceivable.
Previous:C # Interface Converter
Next:Achieved in the C # port reuse Socket
User Reviews
Related Articles
Recommended article
AD