简体   繁体   中英

How can I show MessageBox with suggestion of saving changes when I'm killing program from task manager?

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?

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.

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.

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 ;
            }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM