我怎样才能prevent从关我的C#应用​​程序的用户? [英] How can I prevent a user from closing my C# application?

查看:235
本文介绍了我怎样才能prevent从关我的C#应用​​程序的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中未关闭的应用程序?我想从关闭它,以及禁用的形式,prevent Windows任务管理器中的X按钮。

How to make unclosed application in C#? I want to disable the 'X' button of the form and prevent the Windows Task Manager from closing it as well.

我知道一个办法prevent自闭的形式来处理的FormClosing 事件,但我怎么prevent在关闭任务管理器它?

I know that one way to prevent a form from closing is to handle the FormClosing event, but how do I prevent the task manager from closing it?

推荐答案

没有,那是不可能的prevent任务管理器的关闭应用程序。任务管理器强行终止不响应的过程;它不需要应用程序的权限关闭,并且它不要求很好,无论是。 (请参阅<一个href="http://stackoverflow.com/questions/10765197/what-is-the-difference-between-closing-an-application-and-ending-the-process-fro/10765274#10765274">this回答更多关于任务管理器,应用程序可以被关闭的不同方法之间的比较。)

No, it is not possible to prevent Task Manager from closing your application. Task Manager can forcibly terminate a process that is not responding; it doesn't need the application's permission to close it, and it doesn't ask nicely, either. (See this answer for more on Task Manager, and a comparison between the different ways that an application can be closed.)

唯一可能的解决方法是有两个过程,其中每一个被配置为检测何时另一个被关闭,并启动一个新的实例。当然,这仍然不会被杀死停止进程中的一个,它只是让你重新启动它。而这很可能落入用户敌对行为的范畴。如果我已经使出使用任务管理器来关闭应用程序,我可能希望它消失了,无论你是程序员预期。而且我保证,如果它不断旋转起来的新工艺是疯了(也有可能是疯了就是我的病毒扫描程序,因为它看到这种行为之前)。

The only conceivable workaround is to have two processes, each of them configured to detect when the other one is closed and start up a new instance. Of course, this still won't stop one of the processes from being killed, it will just allow you to restart it. And this probably falls into the category of user-hostile behavior. If I've resorted to using the Task Manager to close your app, I probably want it gone, no matter what you as the programmer intended. And I'm guaranteed to be mad if it keeps spinning up new processes (also likely to be mad is my virus scanner, because it's seen this kind of behavior before).

我建议你重新考虑你的应用程序的设计。如果您需要一台运行所有的时间在后台的东西,你应该创建一个Windows服务。当然,服务不具有用户接口,并且它似乎应用程序要求之一。所以更好的是,防守写你的code:保存应用程序的状态,以便它可以被关闭,并在会恢复。你必须处理的计算机正在关闭,反正如此,因此有多难处理只是你的应用程序被关闭?

I recommend that you reconsider your application's design. If you need something that runs all the time in the background, you should be creating a Windows Service. Of course, services don't have a user interface, and it appears that your application requires one. So better yet, write your code defensively: Save the application's state so that it can be closed and restored at will. You have to handle the case where the computer is shutting down anyway, so how hard is it to handle just your app being shut down?

由于微软的雷蒙德会告诉你,Windows没有一个机制对于这一点,因为没有人能想象一个应用程序的真棒和你没有用户真的愿意收。

As Microsoft's Raymond Chen would tell you, Windows doesn't have a mechanism for this because no one could have imagined an app as awesome as yours that no user would ever want to close.

据禁用窗体的关闭框,系统/窗口菜单中的关闭图标,和<大骨节病>替代 + <大骨节病> F4 按键,这是相对比较简单。你需要重写窗体的 的CreateParams 属性和设置的 CS_NOCLOSE 窗口类风格

As far as disabling your form's close box, the close icon in the system/window menu, and the Alt+F4 keystroke, this is relatively straightforward. You'll need to override your form's CreateParams property, and set the CS_NOCLOSE window class style:

protected override CreateParams CreateParams
{
   get
   {
      const int CS_NOCLOSE = 0x200;

      CreateParams cp = base.CreateParams;
      cp.ClassStyle |= CS_NOCLOSE;
      return cp;
   }
}

编译并运行。你会得到看起来像这样的一种形式(请注意在标题栏的禁用关闭按钮,并没有一个关闭菜单项,在系统/窗口菜单):

Compile and run. You'll get a form that looks like this (note the disabled close button on the titlebar and the absence of a "Close" menu item in the system/window menu):

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

     

请注意,这样做的时候,你应该为你的应用程序界面中的替代机制,以关闭表单。例如,在主的形式从中显示该对话框。

Note that when doing this, you should really provide an alternative mechanism within your application's interface to close the form. For example, on the "master" form from which this dialog was displayed.

这篇关于我怎样才能prevent从关我的C#应用​​程序的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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