C#.net winforms 应用 Sessionending 事件 [英] C#.net winforms application Sessionending event

查看:28
本文介绍了C#.net winforms 应用 Sessionending 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SystemEvents.SessionEnding 当我关闭系统时没有触发事件...

SystemEvents.SessionEnding event is not getting fired when i shut down my system...

推荐答案

你有没有试过把这个事件作为微软的例子来实现?就这样

Have you tried to implement this event as the example of microsoft ? like that

重要提示:控制台应用程序不会引发 SessionEnding 事件.

Important: Console applications do not raise the SessionEnding event.

仅当消息泵正在运行时才会引发此事件.在 Windows 服务中,除非使用隐藏表单或手动启动消息泵,否则不会引发此事件.有关演示如何使用 Windows 服务中的隐藏表单处理系统事件的代码示例,请参阅 SystemEvents 类.->.NET Windows 服务中的消息泵

This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class. -> Message pump in .NET Windows service

private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg==WM_QUERYENDSESSION)
    {
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(ref m);

} //WndProc 

private void Form1_Closing(
    System.Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (systemShutdown)
        // Reset the variable because the user might cancel the 
        // shutdown.
    {
        systemShutdown = false;
        if (DialogResult.Yes==MessageBox.Show("My application", 
            "Do you want to save your work before logging off?", 
            MessageBoxButtons.YesNo))
        {
            e.Cancel = true;
        }
        else
        {
            e.Cancel = false;
        }
    }
}

更多信息?看这里 :SystemEvents.SessionEnding 事件

这篇关于C#.net winforms 应用 Sessionending 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆