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

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.

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?


Solution 1:

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).

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?

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.


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):

     Form with CS_NOCLOSE style

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.

Solution 2:

You should never do things like this, but if you really need this you should create a service.

Solution 3:

This isn't an answer to the question, but I think it might be important to point out that it is not always invalid to create an application that the user has to really go to great lengths to terminate. In the case of an internal private application which is designed to run on machines owned by a company and operated by employees of the company who use the app to do their work as employees, it would be perfectly legitimate to force the app to always stay running. @TvmMurthy question appears to be dealing with exactly this situation.

In my own work, MS Outlook is started up as one of my workstation's startup apps, and I leave it running all day, and into multiple days until I reboot the machine. I have also written Windows Form apps that keep me informed about the status of processes I am responsible to monitor on some of our servers. I wrote these apps to specifically stay running all the time, and only manifest themselves as popups when something is detected that I need to know about (they minimize to the toolbar to keep them out of the way). I use SharpReader for keeping up with my RSS feeds, and it is naturally designed to run constantly. It minimizes to the toolbar, even if you click the red X, and can only be terminated by a context menu from the toolbar or by use of the Task Manager -- and this is perfectly reasonable, even expected, given its function!

Devs should not make the assumption that all applications are voluntarily installed and used by users who are free to run whatever software they desire and when they desire to use it, or that their own preferences are what govern the industry or other users. It is Business Requirements that govern application behavior, not prejudices of developers. As a developer working in industry, I've been required to write lots of behaviors into software that I wouldn't have written had I been writing the software for myself.

Downvoting a question because you don't like what the user has been clearly directed to design into his or her application displays a kind of pettiness or closemindedness that is not particularly commendable. Upvoted question to counteract this.