0

I'm making a program and want show MessageBox with suggestion of saving changes, when I'm trying to kill it from task manager. How can I do it?

Pete
  • 57,112
  • 28
  • 117
  • 166
Jam
  • 101
  • 11
  • Your form has an event handler for Form.Closing. In this event handler you receive the reason for the closing. Just handle it. You can even stop the closing. http://stackoverflow.com/questions/1623756/detect-reason-for-form-closing – Steve May 05 '16 at 07:33
  • Thanks very much Steve! I tried the code that you linked me, and it's work great :) – Jam May 05 '16 at 08:07
  • It works when i'm finishing program from task manager. Can i do something like this when i'm finishing a process from TM ? – Jam May 05 '16 at 08:14

3 Answers3

2

When killing an application from Task Manager you are simply terminating the application without continuing with the code. This means no more code execution. That cannot be handled.

It's like telling an employee "you are fired with immediate effect, pack and leave now" but still expect them to finish writing the application that will take 6 months to complete.

RealSollyM
  • 1,530
  • 1
  • 22
  • 35
1

There is NO way you can execute any code in your application when it is being Killed by operating system or user. That's why its called Killing.

SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

In your principal form you can use even FormClosing as :

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

            DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
            if(dialogResult == DialogResult.Yes)
            {
                //do something
            }
            else if (dialogResult == DialogResult.No)
            {
                e.Cancel=true ;
            }
     }
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17