I have a program that uses the X(close) button in the right hand corner of the Form to make the Form invisible. I only want the program to exit from the Close event if windows is shutting down.

To check if windows is shutting down I override WndProc and check for the WM_ENDSESSION message. That part works perfecly and I set m.Result to (IntPtr)1. Which should allow Windows to continue shutting down. If 1 is wrong I also tried 0 with the same results.

The problem is that the program does not close and windows does not shut down. I am thinking that maybe the Close event is called before the ENDSESSION message is sent and that is why this is not working.

Does anyone have a solution to this problem?