C#Windows应用程序阻止Windows关机/注销 [英] C# Windows application prevents Windows from shutting down / logging off

查看:994
本文介绍了C#Windows应用程序阻止Windows关机/注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个C#Windows窗体应用程序,而不是服务(当用户登录并具有图形用户界面,它只是使用)具有无限循环运行的后台线程。



当我尝试关闭Windows(7),但是,它告诉我的计划是防止它关闭或注销,并询问我是否要强制关机。



现在,是否有可能对我的程序意识到(得到处理)的视窗试图戒烟,或注销?



所以,我需要的是让当Windows试图退出应用程序实现的。



在此先感谢。



编辑:感谢伟大的意见!它是在任何可能的方式使用了这个念头的形式关闭事件,如果它有一个取消的事件处理程序?


解决方案

 公共Form1中()
{
的InitializeComponent();

this.FormClosing + =新FormClosingEventHandler(Form1_FormClosing);
}

无效Form1_FormClosing(对象发件人,FormClosingEventArgs E)
{
//或任何适合你要完成
什么其他的原因如果(e.CloseReason == CloseReason.WindowsShutDown)
{
//停止你的无限循环
}
}


I have written a C# Windows Forms application, not a service (it is only used when the user is logged in and has a graphical user interface) that has a background thread running in an infinite loop.

When I try shutting down Windows (7) however, it tells me the program is preventing it from shutting down or logging off and asks me whether I want to force a shutdown.

Now, is there any possibility for my program to become aware (get a handler) of Windows trying to quit it or to log off?

So, what I need is to make the application realize when Windows tries to quit.

Thanks in advance.

EDIT: Thanks for the great advice! Is it in any way possible to use the idea with the form closing event if it has a CANCEL event handler?

解决方案

public Form1()
{
    InitializeComponent();

    this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}

void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Or any of the other reasons suitable for what you want to accomplish
    if (e.CloseReason == CloseReason.WindowsShutDown)
    {
        //Stop your infinite loop
    }
}

这篇关于C#Windows应用程序阻止Windows关机/注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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