locked
How do I prevent my app closing by task manager - C# RRS feed

  • Question

  • Hi,

    I'm writing a performance counter tool for Windows Vista & Seven os. It's logging memory/cpu usage and display logs( message ) in every 1 hour to current user.

    I want to write this program with rock-solid controls. I mean; User can close program by clicking exit button. But I want to prevent my application from task manager kill operations etc. Because, my application do a lot of things before the exit ( stops threads, clear old logs, write end-notes & comments to current logs etc. )

    So, How can I prevent my application, closing by task manager etc ?

    Here is what I'm trying to do for "closing"( Below codes doesn't work on o.s. with logged by administrator account );

    *The program will to work on administrator accounts. It have to...

            protected override void OnFormClosing(FormClosingEventArgs e)
            {
                switch (e.CloseReason)
                {
                    case CloseReason.ApplicationExitCall:
                        e.Cancel = false;
                        break;
                    case CloseReason.FormOwnerClosing:
                        e.Cancel = true;
                        break;
                    case CloseReason.MdiFormClosing:
                        e.Cancel = true;
                        break;
                    case CloseReason.None:
                        e.Cancel = true;
                        break;
                    case CloseReason.TaskManagerClosing:
                        e.Cancel = true;
                        break;
                    case CloseReason.UserClosing:
                        e.Cancel = false;
                        break;
                    case CloseReason.WindowsShutDown:
                        e.Cancel = false;
                        break;
                    default:
                        MessageBox.Show("Error: Unexpected Close Reason...");
                        break;
                }
    
                base.OnFormClosing(e);
            }
    

    Saturday, March 20, 2010 5:37 PM