Results 1 to 9 of 9

Thread: Cannot close my program for shutdown

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Cannot close my program for shutdown

    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?

  2. #2
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    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"?

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Application.Exit doesn't work? It would be clean.
    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.

  4. #4

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    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:
    Code:
    protected override void WndProc(ref Message m)
    {
      if(a.Environments.ShuttingDown(this.Handle, ref m, true))
      {
        this.notifyIcon.Visible=false;
        Application.Exit();
      }
    base.WndProc(ref m);
    
    //The a.Environments.ShuttingDown method:
    public static bool shuttingDown=false;
    			/// <summary>
    			/// 
    			/// </summary>
    			public static bool ShuttingDown(IntPtr handle, ref Message m, bool returnOkToShutDown)
    			{
    				if(m.Msg==(int)wm.ENDSESSION)
    				{
    					if(returnOkToShutDown && !shuttingDown)
    					{
    						shuttingDown=true;
    						m.Result=(IntPtr)1;
    						//a.Api.SendMessage(handle, (uint)wm.ENDSESSION, (uint)m.WParam, (uint)m.LParam);
    					}
    					return true;
    				}
    				else return false;
    			}
    }
    
    
    //The Closing event:
    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    		{
    			if(a.Environments.shuttingDown)
    			{
    				//this.AskToSave();
    				Application.Exit();
    			}
    			else
    			{
    				e.Cancel=true;
    				this.Visible=false;
    			}
    }
    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.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Here is the program if you are interested in seeing what it does. There is nothing about it that could harm your files or computer or you in any way.
    Attached Files Attached Files

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    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.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The default reaction to QUERYENDSESSION (if you don't catch it) is to allow windows to close, I believe. And maybe to send your main window a WM_CLOSE too.
    http://msdn.microsoft.com/library/de...endsession.asp
    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.

  9. #9

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thanks CornedBee. You have been very helpful. Now I know!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width