1

I know that I can cancel the event of a user clicking the x button in a windows form but I want to make it so that Task Manager will not be able to close it. I haven't found this on the web, but I am sure there is a way to do it.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(e.CloseReason == CloseReason.TaskManagerClosing)
    {
        e.Cancel = true;

    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
jister
  • 135
  • 3
  • 13
  • 2
    Why would you want to do that? That's an essential function of Task Manager. If you could describe what you're trying to accomplish, maybe an alternative can be suggested. I'm thinking you're writing a utility for a kiosk or something like that, where you don't want regular users messing with the system? For something like that, just log the user in as a guest. – Bob Kaufman Aug 03 '13 at 13:44
  • 6
    Task Manager asks nicely first. You block that. Then it switches to murder-death-kill mode. And terminates your process with TerminateProcess(). You can't stop that. The user *always* wins. – Hans Passant Aug 03 '13 at 14:43
  • 1
    "I am sure there is a way to do it." And I'm sure you're wrong. There is no way to prevent a user from closing your application. You can make it more difficult, but ultimately the user can kill your app. – Jim Mischel Aug 03 '13 at 15:22

1 Answers1

2

There are couple of scenarios here:

  1. You kill the application by doing "End Process" from the Processes tab. In this case, you can't do anything.

  2. You do "End Task" from the Applications tab. You can intercept this by handling the closing event of your main form and writing this code. You can read more about close reason here.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.TaskManagerClosing)
        {
    
        }
    }
    

Though if you absolutely want to make sure that user can't stop your application from running, you should create a separate application that keeps a watch on your application. And whenever it is killed, your application runs it again. You can check if application is running or not through following code and call this code through timer after every 10s or any desired interval.

private bool IsProcessRunning(string name)
{
     foreach (Process p in Process.GetProcesses())
     {
            if (p.ProcessName.Contains(name))
            {
                return true;
            }
      }
      return false;
 }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • I don't understand how to implement? – jister Aug 03 '13 at 13:44
  • if(e.CloseReason == CloseReason.TaskManagerClosing) { e.Cancel = true; } Still doesnt do the trick, I have tried this – jister Aug 03 '13 at 13:46
  • @jister where are you closing your form from? processes tab or Applications tab? – Ehsan Aug 03 '13 at 13:50
  • @jister and you have written the above code in the form closing event of your main form? – Ehsan Aug 03 '13 at 13:52
  • See my edit to my orig post... – jister Aug 03 '13 at 13:53
  • have you placed a debugger to see if your code is hit? and i have updated my answer as well – Ehsan Aug 03 '13 at 14:33
  • "You should create a separate application . . ." So all the user has to do is close *that* application first. – Jim Mischel Aug 03 '13 at 15:23
  • @JimMischel you didn't get my point here. Both of the applications (main and the watcher will be configured to watch other and restart if closed) – Ehsan Aug 03 '13 at 15:36
  • How do I make an application watch another? – jister Aug 03 '13 at 16:21
  • @jister i have update my answer with the required code. – Ehsan Aug 03 '13 at 17:09
  • 1
    No, you don't get my point. They can poll more often, but that would seriously impact performance, so I could probably kill them one at a time. Or I could write a script to kill both processes concurrently. Or I could press the Big Red Switch, boot from USB, and delete both programs from my hard drive. Which is what any reasonable user should do to such an ill-behaved program. – Jim Mischel Aug 03 '13 at 22:19
  • @JimMischel We can do as much as we can, when an application is deployed at clients PC it is totally in his control. – Ehsan Aug 04 '13 at 11:22