How can I prevent a user from closing my C # application?

How to make a closed application in C #? I want to disable the "X" button of the form and not let Windows Task Manager also close it.

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

+4
source share
4 answers

No, this does not prevent the Task Manager application from closing. Task Manager may forcefully terminate a process that is not responding; he does not need permission to the application to close it, and it will not ask either. (See this answer for more information on the task manager and a comparison between the different ways to close the application.)

The only possible workaround is to have two processes, each configured to detect when the other is closed, and start a new instance. Of course, this still will not stop one of the processes from killing him, he will simply allow you to restart it. And this probably falls into the category of aggressive user behavior. If I resorted to using the task manager to close the application, I probably want it to disappear, no matter what you are as a programmer. And I guarantee to be insane if he continues to deploy new processes (he will also probably be crazy, this is my antivirus, because he had seen similar behavior before).

I recommend that you review your app design. If you need something that runs all the time in the background, you must create a Windows service. Of course, services do not have a user interface, and it looks like your application requires it. So even better, write your code on the defensive: save the state of the application so that it can be closed and restored at will. You should handle the case when the computer shuts down anyway, since it is difficult to cope with the fact that the application is closed?

As Microsoft Raymond Chen tells you, Windows does not have a mechanism for this, because no one could imagine an application as awesome as yours, which no one would want to close.


As for disabling the form closing window, the closing icon in the system / window menu, and pressing the Alt + F4 key, this is relatively simple. You need to override the CreateParams property form 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 will get a form that looks like this (pay attention to the closed close button on the title bar and the absence of the "Close" menu item in the system / window menu):

Form with CS_NOCLOSE style

Please note that when doing this you must really provide an alternative mechanism in your application interface to close the form. For example, in the form "master" from which this dialog was displayed.

+23
source

You should never do such things, but if you really need it, you must create a service.

+9
source

This is not an answer to the question, but I think it would be important to indicate that it is not always invalid to create an application that the user really has to execute to the end. In the case of an internal private application that is designed to run on machines owned by the company and managed by company employees who use the application to perform their work as employees, it would be perfectly legal to force the application to always remain operational, Question @TvmMurthy seems to relate precisely this situation.

In my own work, MS Outlook starts as one of my boot applications for the workstation, and I leave it to work all day and a few days before the computer restarts. I also wrote Windows Form applications that inform me of the status of the processes I am responding to for monitoring on some of our servers. I wrote these applications so that they constantly work and only display as pop-ups when something is discovered that I need to know about (they minimize the toolbar to keep them to the side). I use SharpReader for keeping up with my RSS feeds, and it is naturally intended to run constantly. It minimizes the toolbar, even if you click on the red X, and it can only be terminated by the context menu on the toolbar or using the task manager - and this is quite reasonable, even if it is expected, given its function!

Devs should not assume that all applications are voluntarily installed and used by users who are free to run any software they want and when they want to use it, or that their own preferences depend on the industry or other users. It is business requirements that drive application behavior, not developer prejudice. As an industry developer, I needed to write many behaviors in software that I would not write if I wrote software for myself.

Reschedule the question, because you don’t like what the user explicitly directed to the design in his application, demonstrates a kind of pettiness or shortsightedness that do not deserve special approval. A simplified question to counteract this.

+4
source

It is not difficult to protect the process from killing ordinary users. An application can modify its own ACL process, which prohibits ANY task manager to kill it. They just get "Access denied!" when trying to kill a process.

However, you should always allow administrators to kill the process so that it does not interfere with shutting down the system or other administrative tasks.

I could provide you the code if it is not needed yet.

0
source

All Articles