To capture Windows shutdown events using C #

Source: Internet
Author: User
Tags static class valid win32
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?



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.