使用c#捕获windows的关机事件

在公司上班,下班时需要签退,而我呢隔三差五就会忘那么一次。怎么办呢,于是就想能不能捕获windows的关机事件,做一个程序让它在关机的时候提醒我一下呢。

 

非常幸运很容易就找到了Microsoft.Win32命名空间下面的SystemEvents类,他有一个静态的事件SessionEnding在系统注销或者关机时发生,此事件只有在winform的程序下有效,而在控制台程序下面无效,不能激发事件;还有一点我们必须在程序推出时将加上的事件移除掉,否则就容易造成内存溢出。

 

关键代码如下:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
using  Microsoft.Win32;
None.gif
None.gif
namespace  Shutdown
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.EnableVisualStyles();
InBlock.gif            Application.SetCompatibleTextRenderingDefault(
false);
InBlock.gif            FormShutdown formShutdown 
= new FormShutdown();
InBlock.gif            SystemEvents.SessionEnding 
+= new SessionEndingEventHandler(formShutdown.SystemEvents_SessionEnding);
InBlock.gif            Application.Run(formShutdown);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
Form 的代码:
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  Microsoft.Win32;
None.gif
None.gif
namespace  Shutdown
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class FormShutdown : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
const string MESSAGE_TXT = "您签退了吗?";
InBlock.gif        
const string MESSAGE_TITLE = "提示";
InBlock.gif
InBlock.gif        
public FormShutdown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
internal void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DialogResult result 
= MessageBox.Show(MESSAGE_TXT, MESSAGE_TITLE, MessageBoxButtons.YesNo);
InBlock.gif
InBlock.gif            e.Cancel 
= (result == DialogResult.No);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void FormShutdown_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - 2000);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnClosed(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SystemEvents.SessionEnding 
-= new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
InBlock.gif            
base.OnClosed(e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

此程序在使用c#2.0在Windows2003下测试通过。大家在使用SystemEvents.SessionEnding事件时切记要在程序退出时移除事件。

不过有两点遗憾之处:

1.  使用这种方式不能捕获休眠时的事件

2.  这个程序占用的内存太多了,只有这么一个小功能居然占了12M的内存,这都是.Net framework惹的货;实在是不可思议。

大家有没有什么好主意可以克服这两个缺点呢?

下载源文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值