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.
Have you investigated using Process.Kill to just kill your app? I would have thought the hard part would have been trapping for system shutdown (which you seem to have working). Or does your app need to exit "cleanly"?
I got the program to close by immediately calling Application.Exit() in the WndProc but windows still does not continue shutting down. Here is some code:
Here is the reason I think why. The Closing event is called BEFORE the WndProc recieves the ENDSESSION message. So, closing is cancelled and at that point, shutdown stops. The ENDSESSION message is finally recieved by WndProc and then the program exits. But it is too late.
So, I think the whole problem is that the program attempts to exit when Shutdown is taking place and the messsage to the app saying that Shutdown is taking place is sent too late.
Do you think I can catch the ENDSESSION message by calling Application.DoEvents() in the Closing event? Or could there be a better way?
Last edited by aewarnick; Oct 20th, 2003 at 06:03 PM.
Don't have .Net or the permission to install it here.
I think that Application.Exit sends a WM_QUIT message and thus ends the message loop, so it shouldn't ignore any messages, as all messages in the queue are first handled before the loop stops.
But maybe I'm wrong.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Thanks guys. I got everything working by catching QUERYENDSESSION instead. I know that it is called before ENDSESSION but I don't understand why windws tries to close the program before it sends the ENDSESSION message.