public override void OnClosing(ClosingEventArgs eventArgs) { if (this.HasBeenClosed) { return; } // save the last size of the window so we can restore it next time. ApplicationSettings.Instance.set(ApplicationSettingsKey.MainWindowMaximized, this.Maximized.ToString().ToLower()); if (!this.Maximized) { ApplicationSettings.Instance.set(ApplicationSettingsKey.WindowSize, string.Format("{0},{1}", Width, Height)); ApplicationSettings.Instance.set(ApplicationSettingsKey.DesktopPosition, string.Format("{0},{1}", DesktopPosition.x, DesktopPosition.y)); } //Save a snapshot of the prints in queue QueueData.Instance.SaveDefaultQueue(); // If we are waiting for a response and get another request, just cancel the close until we get a response. if (exitDialogOpen) { eventArgs.Cancel = true; } string caption = null; string message = null; if (!ApplicationController.Instance.ApplicationExiting && !exitDialogOpen && ApplicationController.Instance.ActivePrinter.Connection.PrinterIsPrinting) { if (ApplicationController.Instance.ActivePrinter.Connection.CommunicationState != CommunicationStates.PrintingFromSd) { caption = "Abort Print".Localize(); message = "Are you sure you want to abort the current print and close MatterControl?".Localize(); } else { caption = "Exit while printing".Localize(); message = "Are you sure you want exit while a print is running from SD Card?\n\nNote: If you exit, it is recommended you wait until the print is completed before running MatterControl again.".Localize(); } } #if !__ANDROID__ else if (PartsSheet.IsSaving()) { caption = "Confirm Exit".Localize(); message = "You are currently saving a parts sheet, are you sure you want to exit?".Localize(); } #endif if (caption != null) { // Record that we are waiting for a response to the request to close exitDialogOpen = true; // We need to show an interactive dialog to determine if the original Close request should be honored, thus cancel the current Close request eventArgs.Cancel = true; UiThread.RunOnIdle(() => { StyledMessageBox.ShowMessageBox( (exitConfirmed) => { // Record that the exitDialog has closed exitDialogOpen = false; // Continue with the original shutdown request if exit confirmed by user if (exitConfirmed) { ApplicationController.Instance.ApplicationExiting = true; ApplicationController.Instance.Shutdown(); // Always call PrinterConnection.Disable on exit unless PrintingFromSd PrinterConnection printerConnection = ApplicationController.Instance.ActivePrinter.Connection; switch (printerConnection.CommunicationState) { case CommunicationStates.PrintingFromSd: case CommunicationStates.Paused when printerConnection.PrePauseCommunicationState == CommunicationStates.PrintingFromSd: break; default: printerConnection.Disable(); break; } this.CloseOnIdle(); } }, message, caption, StyledMessageBox.MessageType.YES_NO_WITHOUT_HIGHLIGHT); }); } else { ApplicationController.Instance.ApplicationExiting = true; // Make sure we tell the Application Controller to shut down. This will release the slicing thread if running. ApplicationController.Instance.Shutdown(); } }