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.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; } }
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?




Reply With Quote