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

Thiru G picture Thiru G · Jan 11, 2011 · Viewed 20.1k times · Source

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?

Answer

Cody Gray picture Cody Gray · Jan 11, 2011

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.