Results 1 to 9 of 9

Thread: Cannot close my program for shutdown

Threaded View

  1. #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.

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