惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

博客园 - 血狼

切换ThinkPad Fn键 window.event.keycode值大全 通用分页存储过程 Oracle中将密码有效期由默认的180天修改成“无限制” C#中动态编译某C#文件 C#程序中执行script .Net Reactor 加密的简单例子 MSBuild的相关操作 程序中执行cmd.exe 北京嘉纳博科技有限公司 Windows Service 初始化代码中追加断点问题 C#定时执行某个程序 .Net中关于多个FrameWork的问题 Chilkat 收、送信 合并文件 设置和获取注册表数据 DotNet多个程序集合并工具 PostGres 全文检索 PostGres 中包含触发器的方法
使用c#捕获Windows的关机事件
血狼 · 2008-07-30 · via 博客园 - 血狼

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using Microsoft.Win32;

namespace Shutdown
{
    
static class Program
    
{
        
/**//// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [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 - 2000);
        }


        
protected override void OnClosed(EventArgs e)
        
{
            SystemEvents.SessionEnding 
-= new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
            
base.OnClosed(e);
        }

    }

}

注意:

1、控制台应用程序不会引发 SessionEnding 事件。

2、因为这是一个静态事件,所以释放应用程序时必须分离事件处理程序,否则会导致内存泄漏。

参照

http://www.cnblogs.com/yukaizhao/archive/2007/04/23/csharp_catch_shutdown_events.html 

http://msdn.microsoft.com/zh-cn/library/microsoft.win32.systemevents.sessionending(VS.85).aspx