Example #1
0
        protected sealed override void OnClosing(CancelEventArgs cancelArgs)
        {
            base.OnClosing(cancelArgs);
            if (cancelArgs.Cancel)
            {
                // Base canceled closing because it probably hid and/or minimized the form
                ApplicationExiting = true;
            }
            else
            {
                bool cancelExit = false;
                try {
                    // Set application exiting if not already done so
                    if (!ApplicationExiting)
                    {
                        ApplicationExiting = true;
                    }

                    string cancelReason = string.Empty;

                    // Ask user to confirm exit
                    if (ApplicationServices.AskYN("Are you sure you want to exit?"))
                    {
                        // Now ask form observers to confirm exit
                        FireApplicationExitingEvent(cancelArgs);
                        cancelExit = cancelArgs.Cancel;
                        // If no aborts, ask framework to confirm exit
                        if (!cancelExit)
                        {
                            Sphere10Framework.Instance.EndWinFormsApplication(out cancelExit, out cancelReason);
                        }


                        if (cancelExit)
                        {
                            // An observer or framework exit task somewhere has cancelled the exit.
                            // Ask user to exit anyway
                            if (ApplicationServices.AskYN(ParagraphBuilder.Combine("The application failed to exit properly",
                                                                                   cancelReason ?? string.Empty,
                                                                                   "You may lose unsaved data if you exit", "Exit anyway?")))
                            {
                                // This will force an exit
                                Exit(true);
                            }
                            else
                            {
                                cancelArgs.Cancel = true;
                            }
                        }
                    }
                    else
                    {
                        cancelArgs.Cancel = true;
                    }
                } finally {
                    ApplicationExiting = cancelExit;
                }
            }
        }