1

I have a backup power supply for my computer which is attached inline with it and the wall. When I pull the power cord from the wall, I have 2-5 minutes before the backup supply shuts down the computer. It is during this time that I want to write data to a file with the code below:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
    {
        writeContents("Interrupted");
        sendMessage("PWR - The Spring Test Machine has stopped");                
        return;
    }

    if (e.CloseReason.Equals(CloseReason.UserClosing)) 
    {
        if (MessageBox.Show("You are closing this application.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
        {
            writeContents("Interrupted");
            return;
        }
        else
            e.Cancel = true; 
    } 
}

The problem is that it didn't work. I don't think the the closing event ever got called. Any ideas would greatly be appreciated. Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user112016322
  • 688
  • 3
  • 11
  • 26

3 Answers3

3

From here http://www.daniweb.com/software-development/csharp/threads/253249/application-exit-does-not-trigger-the-formclosing

when you use taskmanager to "kill" the appliction, it doesn't fire any events, It simply stops the execution, This is how you close an application that has frozen. if you waited for it to handle any events, then it would still be frozen.

as for when you restart the computer or shutdown, the event will be called only if there is enough time, they system tells all applications it is shutting down and only gives them a short amount of time to handle business before it kills them. Windows 7 will show a dialog telling you which applications are still busy and ask you if you want to kill them and shutdown, or cancel. But as for XP, it just kills them after X amount of seconds.

That's pretty much how I would have imagined the behavior of FormClosing... Would Application.ApplicationExit Event be a better event to listen for?

Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
1

Here is some usful info about Properties of FormClosing event:

Bug in FormClosingEventArgs.CloseReason?

And you can try using switch statement, like here: Detect reason for form closing

Community
  • 1
  • 1
Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
0

Not sure if it works cuase i need to check it first and im not infront of my machine now .. but have you also tried the other elements of the CloseReason enum ? try TaskManagerClosing . In any case try first to throw a message( messagebox ) to see if you actually have the correct CloseReason you want and also don't forget to use the e.Cancel = true if you dont want the pc to close everytime you check it . After that check if the method you have for writting to the file is ok .

maybe this will help also Prompt user to save when closing app

Community
  • 1
  • 1
isioutis
  • 304
  • 2
  • 13