设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
小说专版  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专   题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: 刺猬宫 >> 网络编程 >> ASP.NET >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
应用实例:ASP.Net中利用…
关于C#静态构造函数的几…
使用Control Adapters优…
C#根据条件搜索Infopath…
使用javascript来实…
新手入门:javascri…
网页制作技巧文章之用CS…
网页标准研究:用CSS实现…
小技巧:用CSS如何实现单…
网页标准学习:利用CSS同…
  使用c#捕获windows的关机事件           
使用c#捕获windows的关机事件
 

  在公司上班,下班时需要签退,而我呢隔三差五就会忘那么一次。怎么办呢,于是就想能不能捕获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惹的货;实在是不可思议。

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

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2007-6-20 17:17:05
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:itqoo    责任编辑:itqoo 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持