All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP中使用crypt()实现用户身份验证_php资料_编程技术
ASP.NET,2.0移动开发入门之基础_[Asp.Net教程]
PHP程序中的特效应用,实用珍藏代码举例_php资料_编程技术
用PHP简易实现中文分词_[PHP教程]
ASP与ASP.NET互通COOKIES的一点经验_[Asp.Net教程]
web.config配置文件示例_[Asp.Net教程]
Asp.net文件上传操作_[Asp.Net教程]
C#教程:读写注册表
ASP.NET技巧:慎用url重写_[Asp.Net教程]
根据IP自动获取所在地天气预报_JavaScript技术_编程技术
Reading number is top 10 pictures
西游日记2
PengLiYuan lady for the world health organization fight TB and AIDS goodwill ambassador
这两天,中国人民到处都可以“看海”了
西方气质的东方美女2
身材野火台灣美女2
云南大理的美女
美女就是美女
八个盛产美女的国家1
穷哥们向美女求婚攻略
Ashlynn Brooke photograph of a group2
Download software ranking
Twenty piece of palm leaf
I'm come from Beijing1
天龙八部十二宫服务端
Unix video tutorial15
Unix video tutorial3
变速齿轮3.26
Tram sex maniac 2 (H) rar bag15
Love the forty days
双旗镇刀客A
卡丁车单机版
aaa published in(发表于) 2013/12/17 7:46:40 Edit(编辑)
使用c#捕获windows的关机事件_.net资料_编程技术

使用c#捕获windows的关机事件_.net资料_编程技术

使用c#捕获windows的关机事件_.net资料_编程技术-你的首页-uuhomepage.com

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


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





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.