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

推荐订阅源

T
The Blog of Author Tim Ferriss
罗磊的独立博客
月光博客
月光博客
GbyAI
GbyAI
腾讯CDC
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
雷峰网
雷峰网
B
Blog RSS Feed
美团技术团队
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
D
Docker

博客园 - 血狼

切换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