使用c#捕获windows的关机事件
2007-06-06 13:10
435 查看
在公司上班,下班时需要签退,而我呢隔三差五就会忘那么一次。怎么办呢,于是就想能不能捕获windows的关机事件,做一个程序让它在关机的时候提醒我一下呢。
非常幸运很容易就找到了Microsoft.Win32命名空间下面的SystemEvents类,他有一个静态的事件SessionEnding在系统注销或者关机时发生,此事件只有在winform的程序下有效,而在控制台程序下面无效,不能激发事件;还有一点我们必须在程序推出时将加上的事件移除掉,否则就容易造成内存溢出。
关键代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
static class Program
{
/**////
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormShutdown formShutdown = new FormShutdown();
SystemEvents.SessionEnding += new SessionEndingEventHandler(formShutdown.SystemEvents_SessionEnding);
Application.Run(formShutdown);
}
}
}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 = "您签退了吗?";
const string MESSAGE_TITLE = "提示";
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);
}
}
}
此程序在使用c#2.0在Windows2003下测试通过。大家在使用SystemEvents.SessionEnding事件时切记要在程序退出时移除事件。
不过有两点遗憾之处:
1. 使用这种方式不能捕获休眠时的事件
2. 这个程序占用的内存太多了,只有这么一个小功能居然占了12M的内存,这都是.Net framework惹的货;实在是不可思议。
非常幸运很容易就找到了Microsoft.Win32命名空间下面的SystemEvents类,他有一个静态的事件SessionEnding在系统注销或者关机时发生,此事件只有在winform的程序下有效,而在控制台程序下面无效,不能激发事件;还有一点我们必须在程序推出时将加上的事件移除掉,否则就容易造成内存溢出。
关键代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
static class Program
{
/**////
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormShutdown formShutdown = new FormShutdown();
SystemEvents.SessionEnding += new SessionEndingEventHandler(formShutdown.SystemEvents_SessionEnding);
Application.Run(formShutdown);
}
}
}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 = "您签退了吗?";
const string MESSAGE_TITLE = "提示";
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);
}
}
}
此程序在使用c#2.0在Windows2003下测试通过。大家在使用SystemEvents.SessionEnding事件时切记要在程序退出时移除事件。
不过有两点遗憾之处:
1. 使用这种方式不能捕获休眠时的事件
2. 这个程序占用的内存太多了,只有这么一个小功能居然占了12M的内存,这都是.Net framework惹的货;实在是不可思议。
相关文章推荐
- 使用c#捕获windows的关机事件
- 使用c#捕获windows的关机事件
- 使用c#捕获windows的关机事件(转载)
- 使用c#捕获windows的关机事件
- 使用c#捕获Windows的关机事件
- 使用c#捕获windows的关机事件
- 使用c#捕获windows的关机事件
- C#捕获windows关机事件,在系统关机前做一些自己想做的事
- C#捕获Windows关机事件
- C#控制台代码获取windows的关机事件。
- C#如何捕获windows关机事件
- 使用c#对windows进行关机、重启或注销
- VC 捕获Windows关机事件
- VC 捕获Windows关机事件
- C#中使用EventLog类写Windows事件日志
- C# 中使用 EventLog类写Windows事件日志
- C# 捕获关机事件方法
- C#接收Windows系统的关机(注销)事件
- WCF 寄宿于Windows服务 ,可能会报的错:在系统启动时至少有一个服务或驱动程序产生错误。详细信息,请使用事件查看器查看事件日志。
- C#编写Windows服务程序 (服务端),客户端使用 消息队列 实现淘宝 订单全链路效果