Window
When I work in a company, I need to sign back from work, and I gesanchaiwu forget that once. What to do, so I would like to be able to capture the shutdown of Windows events, to do a program to let it in the shutdown time to remind me.
Very lucky to have found the Microsoft.Win32 namespace below the SystemEvents class, he has a static event sessionending occurs when the system logs off or shuts down, this event is only valid under the WinForm program, but is not valid under the console program and cannot fire events There is also a point where we have to remove the events that are added when the program is launched, otherwise it can easily cause a memory overflow.
The key code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Windows.Forms;
Using Microsoft.Win32;
Namespace Shutdown
{
Static Class Program
{
/**////
The main entry point for the application.
///
[STAThread]
static void Main ()
{
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);
Formshutdown Formshutdown = new Formshutdown ();
Systemevents.sessionending + = new Sessionendingeventhandler (formshutdown.systemevents_sessionending);
Application.Run (Formshutdown);
}
}
Code for}form:
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 = "Did you sign the refund?";
Const string Message_title = "Prompt";
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);
}
}
}
This program is tested by using c#2.0 under Windows2003. When you use the Systemevents.sessionending event, remember to remove the event when the program exits.
But there are two points of regret:
1. In this way you cannot capture events at hibernation
2. This program occupies too much memory, only such a small function is actually accounted for 12M of memory, this is the. Net framework of goods; it's unbelievable.
Do you have any good ideas to overcome these two shortcomings?